Skip to content

BUG: fix HDFStore.append with all empty strings error (GH12242) #23435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
- Bug in :meth:`detect_client_encoding` where potential ``IOError`` goes unhandled when importing in a mod_wsgi process due to restricted access to stdout. (:issue:`21552`)
- Bug in :func:`to_string()` that broke column alignment when ``index=False`` and width of first column's values is greater than the width of first column's header (:issue:`16839`, :issue:`13032`)
- Bug in :func:`DataFrame.to_csv` where a single level MultiIndex incorrectly wrote a tuple. Now just the value of the index is written (:issue:`19589`).
- Bug in :meth:`HDFStore.append` when appending a :class:`DataFrame` with an empty string column and ``min_itemsize`` < 8 (:issue:`12242`)

Plotting
^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4637,7 +4637,7 @@ def _convert_string_array(data, encoding, errors, itemsize=None):
# create the sized dtype
if itemsize is None:
ensured = ensure_object(data.ravel())
itemsize = libwriters.max_len_string_array(ensured)
itemsize = max(1, libwriters.max_len_string_array(ensured))

data = np.asarray(data, dtype="S%d" % itemsize)
return data
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,16 @@ def check_col(key, name, size):
pytest.raises(ValueError, store.append, 'df',
df, min_itemsize={'foo': 20, 'foobar': 20})

def test_append_with_empty_string(self):

with ensure_clean_store(self.path) as store:

# with all empty strings (GH 12242)
df = DataFrame({'x': ['a', 'b', 'c', 'd', 'e', 'f', '']})
store.append('df', df[:-1], min_itemsize={'x': 1})
store.append('df', df[-1:], min_itemsize={'x': 1})
tm.assert_frame_equal(store.select('df'), df)

def test_to_hdf_with_min_itemsize(self):

with ensure_clean_path(self.path) as path:
Expand Down