Skip to content

Commit

Permalink
TST: Add tests for all None
Browse files Browse the repository at this point in the history
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 committed Nov 15, 2018
1 parent 904576a commit 527a1e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 4 additions & 2 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,12 +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('Only string-like object arrays containing all '
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.')
'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 527a1e3

Please sign in to comment.