Skip to content

Commit e9aee5b

Browse files
bashtagejreback
authored andcommitted
ENH: Improve error message for empty object array (#23718)
* ENH: Improve error message for empty object array Improve the error message shown when an object array is empty closes #23572 * 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 1a42c70 commit e9aee5b

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

pandas/io/stata.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1868,7 +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('Writing general object arrays is not supported')
1871+
raise ValueError('Column `{col}` cannot be exported.\n\nOnly '
1872+
'string-like object arrays containing all '
1873+
'strings or a mix of strings and None can be '
1874+
'exported. Object arrays containing only null '
1875+
'values are prohibited. Other object types'
1876+
'cannot be exported and must first be converted '
1877+
'to one of the supported '
1878+
'types.'.format(col=column.name))
18721879
itemsize = max_len_string_array(ensure_object(column.values))
18731880
if itemsize > max_str_len:
18741881
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)