Skip to content

Commit 4627462

Browse files
committed
Remove excess comments. Reverse error type change to avoid api changes. Move nan_rep tests into separate function.
1 parent 500ab5a commit 4627462

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

pandas/io/pytables.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3524,14 +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-
## Raise clearer error if mismatching type on values_axes
35283527
if c == "values_axes" and sax.kind != oax.kind:
3529-
raise TypeError(
3528+
raise ValueError(
35303529
f"Cannot serialize the column [{oax.values[0]}] "
35313530
f"because its data contents are not [{sax.kind}] "
35323531
f"but [{oax.kind}] object dtype"
35333532
)
3534-
# Fallback if other source of difference
35353533
raise ValueError(
35363534
f"invalid combination of [{c}] on appending data "
35373535
f"[{sax}] vs current table [{oax}]"

pandas/tests/io/pytables/test_append.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,6 @@ def check_col(key, name, size):
421421
with pytest.raises(ValueError, match=msg):
422422
store.append("df_new", df_new)
423423

424-
# bigger NaN representation on next append
425-
df_new = DataFrame([[124, "a"], [346, "b"]])
426-
store.append("df_new2", df_new)
427-
df_new = DataFrame([[124, None], [346, "b"]])
428-
msg = "NaN representation is too large for existing column size"
429-
with pytest.raises(ValueError, match=msg):
430-
store.append("df_new2", df_new)
431-
432424
# min_itemsize on Series index (GH 11412)
433425
df = DataFrame(
434426
{
@@ -835,7 +827,7 @@ def test_append_raise(setup_path):
835827
"because its data contents are not [string] "
836828
"but [datetime64[s]] object dtype"
837829
)
838-
with pytest.raises(TypeError, match=msg):
830+
with pytest.raises(ValueError, match=msg):
839831
store.append("df", df)
840832

841833

@@ -1002,3 +994,29 @@ def test_append_to_multiple_min_itemsize(setup_path):
1002994
)
1003995
result = store.select_as_multiple(["index", "nums", "strs"])
1004996
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def test_table_values_dtypes_roundtrip(setup_path):
217217
"because its data contents are not [float] "
218218
"but [integer] object dtype"
219219
)
220-
with pytest.raises(TypeError, match=msg):
220+
with pytest.raises(ValueError, match=msg):
221221
store.append("df_i8", df1)
222222

223223
# check creation/storage/retrieval of float32 (a bit hacky to

0 commit comments

Comments
 (0)