Skip to content

Commit

Permalink
ENH: Improve error message for empty object array (pandas-dev#23718)
Browse files Browse the repository at this point in the history
* ENH: Improve error message for empty object array

Improve the error message shown when an object array is empty

closes pandas-dev#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)
  • Loading branch information
bashtage authored and Pingviinituutti committed Feb 28, 2019
1 parent c4751ca commit c7d7a30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
9 changes: 8 additions & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,14 @@ def _dtype_to_default_stata_fmt(dtype, column, dta_version=114,
inferred_dtype = infer_dtype(column.dropna())
if not (inferred_dtype in ('string', 'unicode') or
len(column) == 0):
raise ValueError('Writing general object arrays is not supported')
raise ValueError('Column `{col}` cannot be exported.\n\nOnly '
'string-like object arrays containing all '
'strings or a mix of strings and None can be '
'exported. Object arrays containing only null '
'values are prohibited. Other object types'
'cannot be exported and must first be converted '
'to one of the supported '
'types.'.format(col=column.name))
itemsize = max_len_string_array(ensure_object(column.values))
if itemsize > max_str_len:
if dta_version >= 117:
Expand Down
28 changes: 26 additions & 2 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,11 +1514,35 @@ def test_mixed_string_strl(self):
{'mixed': None,
'number': 1}
]

output = pd.DataFrame(output)
output.number = output.number.astype('int32')

with tm.ensure_clean() as path:
output.to_stata(path, write_index=False, version=117)
reread = read_stata(path)
expected = output.fillna('')
expected.number = expected.number.astype('int32')
tm.assert_frame_equal(reread, expected)

# Check strl supports all None (null)
output.loc[:, 'mixed'] = None
output.to_stata(path, write_index=False, convert_strl=['mixed'],
version=117)
reread = read_stata(path)
expected = output.fillna('')
tm.assert_frame_equal(reread, expected)

@pytest.mark.parametrize('version', [114, 117])
def test_all_none_exception(self, version):
output = [
{'none': 'none',
'number': 0},
{'none': None,
'number': 1}
]
output = pd.DataFrame(output)
output.loc[:, 'none'] = None
with tm.ensure_clean() as path:
with pytest.raises(ValueError) as excinfo:
output.to_stata(path, version=version)
assert 'Only string-like' in excinfo.value.args[0]
assert 'Column `none`' in excinfo.value.args[0]

0 comments on commit c7d7a30

Please sign in to comment.