Skip to content

BUG: fix the bad error raised by HDFStore.put() #38919

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 9 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ I/O
- Allow custom error values for parse_dates argument of :func:`read_sql`, :func:`read_sql_query` and :func:`read_sql_table` (:issue:`35185`)
- Bug in :func:`to_hdf` raising ``KeyError`` when trying to apply
for subclasses of ``DataFrame`` or ``Series`` (:issue:`33748`).
- Bug in :func:`put` raising a wrong ``TypeError`` when saving a DataFrame with non-string dtype (:issue:`34274`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be :meth:`~HDFStore.put`

say this is an improved error message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thank you for that
It's morning in my timezone here and I just wake up 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heheh, no prob this PR looks good. just a small change.

- Bug in :func:`json_normalize` resulting in the first element of a generator object not being included in the returned ``DataFrame`` (:issue:`35923`)
- Bug in :func:`read_excel` forward filling :class:`MultiIndex` names with multiple header and index columns specified (:issue:`34673`)
- :func:`pandas.read_excel` now respects :func:``pandas.set_option`` (:issue:`34252`)
Expand Down
43 changes: 38 additions & 5 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3916,6 +3916,7 @@ def get_blk_items(mgr, blocks):
nan_rep=nan_rep,
encoding=self.encoding,
errors=self.errors,
block_columns=b_items,
)
adj_name = _maybe_adjust_name(new_name, self.version)

Expand Down Expand Up @@ -4875,8 +4876,34 @@ def _unconvert_index(


def _maybe_convert_for_string_atom(
name: str, block, existing_col, min_itemsize, nan_rep, encoding, errors
name: str,
block,
existing_col,
min_itemsize,
nan_rep,
encoding,
errors,
block_columns: List[str],
):
"""
Ensure all elements in the given block are "string".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok this is more confusing, let's just remove this doc-string entirely

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry english is not my first language and I don't totally understand why non-strings aren't okay to be written in HDF5.
it's just literally do this thing, Ensure all elements in the given block are "string".
It is now removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the elements are not actual strings but python objects we cann't serialize to this column type.


Parameter
------
name(str):
block:
existing_col:
min_itemsize:
nan_rep:
encoding:
errors:
block_columns(list[str]): the label of columns for debug info use.

Return
------
Value of a block, if its dtype is object and constructed by "string"
elements.
"""
if not block.is_object:
return block.values

Expand Down Expand Up @@ -4908,14 +4935,20 @@ def _maybe_convert_for_string_atom(

# we cannot serialize this data, so report an exception on a column
# by column basis
for i in range(len(block.shape[0])):

# expected behaviour:
# search block for a non-string object column by column
for i in range(block.shape[0]):
col = block.iget(i)
inferred_type = lib.infer_dtype(col, skipna=False)
if inferred_type != "string":
iloc = block.mgr_locs.indexer[i]
error_column_label = (
block_columns[i] if len(block_columns) > i else f"No.{i}"
)
raise TypeError(
f"Cannot serialize the column [{iloc}] because\n"
f"its data contents are [{inferred_type}] object dtype"
f"Cannot serialize the column [{error_column_label}]\n"
f"because its data contents are not [string] but "
f"[{inferred_type}] object dtype"
)

# itemsize is the maximum length of a string (along any dimension)
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,10 @@ def test_append_raise(self, setup_path):
df = tm.makeDataFrame()
df["invalid"] = [["a"]] * len(df)
assert df.dtypes["invalid"] == np.object_
msg = re.escape("object of type 'int' has no len()")
msg = re.escape(
"""Cannot serialize the column [invalid]
because its data contents are not [string] but [mixed] object dtype"""
)
with pytest.raises(TypeError, match=msg):
store.append("df", df)

Expand Down Expand Up @@ -2221,7 +2224,10 @@ def test_unimplemented_dtypes_table_columns(self, setup_path):

with ensure_clean_store(setup_path) as store:
# this fails because we have a date in the object block......
msg = "object of type 'int' has no len()"
msg = re.escape(
"""Cannot serialize the column [datetime1]
because its data contents are not [string] but [date] object dtype"""
)
with pytest.raises(TypeError, match=msg):
store.append("df_unimplemented", df)

Expand Down