Skip to content

Commit 57340ec

Browse files
authored
ENH: Improved error message and raise new error for small-string NaN edge case in HDFStore.append (#60829)
* Add clearer error messages for datatype mismatch in HDFStore.append. Raise ValueError when nan_rep too large for pytable column. Add and modify applicable test code. * Fix missed tests and correct mistake in error message. * Remove excess comments. Reverse error type change to avoid api changes. Move nan_rep tests into separate function.
1 parent 51b12e8 commit 57340ec

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

pandas/io/pytables.py

+9
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,12 @@ def validate(self, other) -> None:
35243524
# Value of type "Optional[Any]" is not indexable [index]
35253525
oax = ov[i] # type: ignore[index]
35263526
if sax != oax:
3527+
if c == "values_axes" and sax.kind != oax.kind:
3528+
raise ValueError(
3529+
f"Cannot serialize the column [{oax.values[0]}] "
3530+
f"because its data contents are not [{sax.kind}] "
3531+
f"but [{oax.kind}] object dtype"
3532+
)
35273533
raise ValueError(
35283534
f"invalid combination of [{c}] on appending data "
35293535
f"[{sax}] vs current table [{oax}]"
@@ -5136,6 +5142,9 @@ def _maybe_convert_for_string_atom(
51365142
data = bvalues.copy()
51375143
data[mask] = nan_rep
51385144

5145+
if existing_col and mask.any() and len(nan_rep) > existing_col.itemsize:
5146+
raise ValueError("NaN representation is too large for existing column size")
5147+
51395148
# see if we have a valid string type
51405149
inferred_type = lib.infer_dtype(data, skipna=False)
51415150
if inferred_type != "string":

pandas/tests/io/pytables/test_append.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -823,12 +823,9 @@ def test_append_raise(setup_path):
823823
store.append("df", df)
824824
df["foo"] = "bar"
825825
msg = re.escape(
826-
"invalid combination of [values_axes] on appending data "
827-
"[name->values_block_1,cname->values_block_1,"
828-
"dtype->bytes24,kind->string,shape->(1, 30)] "
829-
"vs current table "
830-
"[name->values_block_1,cname->values_block_1,"
831-
"dtype->datetime64[s],kind->datetime64[s],shape->None]"
826+
"Cannot serialize the column [foo] "
827+
"because its data contents are not [string] "
828+
"but [datetime64[s]] object dtype"
832829
)
833830
with pytest.raises(ValueError, match=msg):
834831
store.append("df", df)
@@ -997,3 +994,29 @@ def test_append_to_multiple_min_itemsize(setup_path):
997994
)
998995
result = store.select_as_multiple(["index", "nums", "strs"])
999996
tm.assert_frame_equal(result, expected, check_index_type=True)
997+
998+
999+
def test_append_string_nan_rep(setup_path):
1000+
# GH 16300
1001+
df = DataFrame({"A": "a", "B": "foo"}, index=np.arange(10))
1002+
df_nan = df.copy()
1003+
df_nan.loc[0:4, :] = np.nan
1004+
msg = "NaN representation is too large for existing column size"
1005+
1006+
with ensure_clean_store(setup_path) as store:
1007+
# string column too small
1008+
store.append("sa", df["A"])
1009+
with pytest.raises(ValueError, match=msg):
1010+
store.append("sa", df_nan["A"])
1011+
1012+
# nan_rep too big
1013+
store.append("sb", df["B"], nan_rep="bars")
1014+
with pytest.raises(ValueError, match=msg):
1015+
store.append("sb", df_nan["B"])
1016+
1017+
# smaller modified nan_rep
1018+
store.append("sc", df["A"], nan_rep="n")
1019+
store.append("sc", df_nan["A"])
1020+
result = store["sc"]
1021+
expected = concat([df["A"], df_nan["A"]])
1022+
tm.assert_series_equal(result, expected)

pandas/tests/io/pytables/test_round_trip.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,9 @@ def test_table_values_dtypes_roundtrip(setup_path):
213213

214214
# incompatible dtype
215215
msg = re.escape(
216-
"invalid combination of [values_axes] on appending data "
217-
"[name->values_block_0,cname->values_block_0,"
218-
"dtype->float64,kind->float,shape->(1, 3)] vs "
219-
"current table [name->values_block_0,"
220-
"cname->values_block_0,dtype->int64,kind->integer,"
221-
"shape->None]"
216+
"Cannot serialize the column [a] "
217+
"because its data contents are not [float] "
218+
"but [integer] object dtype"
222219
)
223220
with pytest.raises(ValueError, match=msg):
224221
store.append("df_i8", df1)

0 commit comments

Comments
 (0)