Skip to content

Commit b95f65b

Browse files
BUG: Fix regression in to_hdf with MultiIndex and StringDtype (GH#63412) (#63414)
1 parent 9694eb1 commit b95f65b

File tree

5 files changed

+13
-19
lines changed

5 files changed

+13
-19
lines changed

pandas/io/pytables.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,9 @@ def write_multi_index(self, key: str, index: MultiIndex) -> None:
31473147
zip(index.levels, index.codes, index.names, strict=True)
31483148
):
31493149
# write the level
3150-
if isinstance(lev.dtype, ExtensionDtype):
3150+
if isinstance(lev.dtype, ExtensionDtype) and not isinstance(
3151+
lev.dtype, StringDtype
3152+
):
31513153
raise NotImplementedError(
31523154
"Saving a MultiIndex with an extension dtype is not supported."
31533155
)

pandas/tests/io/pytables/test_put.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,6 @@ def test_column_multiindex(tmp_path, setup_path, using_infer_string):
314314

315315
path = tmp_path / setup_path
316316
with HDFStore(path) as store:
317-
if using_infer_string:
318-
# TODO(infer_string) make this work for string dtype
319-
msg = "Saving a MultiIndex with an extension dtype is not supported."
320-
with pytest.raises(NotImplementedError, match=msg):
321-
store.put("df", df)
322-
return
323317
store.put("df", df)
324318
tm.assert_frame_equal(
325319
store["df"], expected, check_index_type=True, check_column_type=True

pandas/tests/io/pytables/test_read.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,6 @@ def test_read_hdf_open_store(tmp_path, setup_path, using_infer_string):
181181
df = df.set_index(keys="E", append=True)
182182

183183
path = tmp_path / setup_path
184-
if using_infer_string:
185-
# TODO(infer_string) make this work for string dtype
186-
msg = "Saving a MultiIndex with an extension dtype is not supported."
187-
with pytest.raises(NotImplementedError, match=msg):
188-
df.to_hdf(path, key="df", mode="w")
189-
return
190184
df.to_hdf(path, key="df", mode="w")
191185
direct = read_hdf(path, "df")
192186
with HDFStore(path, mode="r") as store:

pandas/tests/io/pytables/test_round_trip.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,6 @@ def test_store_hierarchical(
434434
):
435435
frame = multiindex_dataframe_random_data
436436

437-
if using_infer_string:
438-
# TODO(infer_string) make this work for string dtype
439-
msg = "Saving a MultiIndex with an extension dtype is not supported."
440-
with pytest.raises(NotImplementedError, match=msg):
441-
_check_roundtrip(frame, tm.assert_frame_equal, path=temp_file)
442-
return
443437
_check_roundtrip(frame, tm.assert_frame_equal, path=temp_file)
444438
_check_roundtrip(frame.T, tm.assert_frame_equal, path=temp_file)
445439
_check_roundtrip(frame["A"], tm.assert_series_equal, path=temp_file)

pandas/tests/io/pytables/test_store.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,3 +1129,13 @@ def test_select_categorical_string_columns(tmp_path, model):
11291129
result = store.select("df", "modelId == model")
11301130
expected = df[df["modelId"] == model]
11311131
tm.assert_frame_equal(result, expected)
1132+
1133+
1134+
def test_to_hdf_multiindex_string_dtype_crash(tmp_path):
1135+
# GH#63412
1136+
path = tmp_path / "test.h5"
1137+
index = MultiIndex.from_tuples([("a", "x"), ("b", "y")], names=["level1", "level2"])
1138+
df = DataFrame({"value": [1, 2]}, index=index)
1139+
df.to_hdf(path, key="test")
1140+
result = read_hdf(path, key="test")
1141+
tm.assert_frame_equal(df, result, check_dtype=False)

0 commit comments

Comments
 (0)