Skip to content

Commit a1c3f48

Browse files
committed
TST: Add tests for all None
Test exception is hit when all values in an object column are None Extend the test for strl conversion to ensure this case passes (as expected)
1 parent 3eb95fd commit a1c3f48

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pandas/io/stata.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1868,12 +1868,14 @@ def _dtype_to_default_stata_fmt(dtype, column, dta_version=114,
18681868
inferred_dtype = infer_dtype(column.dropna())
18691869
if not (inferred_dtype in ('string', 'unicode') or
18701870
len(column) == 0):
1871-
raise ValueError('Only string-like object arrays containing all '
1871+
raise ValueError('Column `{col}` cannot be exported.\n\nOnly '
1872+
'string-like object arrays containing all '
18721873
'strings or a mix of strings and None can be '
18731874
'exported. Object arrays containing only null '
18741875
'values are prohibited. Other object types'
18751876
'cannot be exported and must first be converted '
1876-
'to one of the supported types.')
1877+
'to one of the supported '
1878+
'types.'.format(col=column.name))
18771879
itemsize = max_len_string_array(ensure_object(column.values))
18781880
if itemsize > max_str_len:
18791881
if dta_version >= 117:

pandas/tests/io/test_stata.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -1514,11 +1514,35 @@ def test_mixed_string_strl(self):
15141514
{'mixed': None,
15151515
'number': 1}
15161516
]
1517-
15181517
output = pd.DataFrame(output)
1518+
output.number = output.number.astype('int32')
1519+
15191520
with tm.ensure_clean() as path:
15201521
output.to_stata(path, write_index=False, version=117)
15211522
reread = read_stata(path)
15221523
expected = output.fillna('')
1523-
expected.number = expected.number.astype('int32')
15241524
tm.assert_frame_equal(reread, expected)
1525+
1526+
# Check strl supports all None (null)
1527+
output.loc[:, 'mixed'] = None
1528+
output.to_stata(path, write_index=False, convert_strl=['mixed'],
1529+
version=117)
1530+
reread = read_stata(path)
1531+
expected = output.fillna('')
1532+
tm.assert_frame_equal(reread, expected)
1533+
1534+
@pytest.mark.parametrize('version', [114, 117])
1535+
def test_all_none_exception(self, version):
1536+
output = [
1537+
{'none': 'none',
1538+
'number': 0},
1539+
{'none': None,
1540+
'number': 1}
1541+
]
1542+
output = pd.DataFrame(output)
1543+
output.loc[:, 'none'] = None
1544+
with tm.ensure_clean() as path:
1545+
with pytest.raises(ValueError) as excinfo:
1546+
output.to_stata(path, version=version)
1547+
assert 'Only string-like' in excinfo.value.args[0]
1548+
assert 'Column `none`' in excinfo.value.args[0]

0 commit comments

Comments
 (0)