Skip to content

BUG: to_hdf and HDFStore for subclasses #38262

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 12 commits into from
Dec 19, 2020
6 changes: 4 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,10 @@ def error(t):
"nor a value are passed"
)
else:
_TYPE_MAP = {Series: "series", DataFrame: "frame"}
pt = _TYPE_MAP[type(value)]
if isinstance(value, Series):
Copy link
Member

Choose a reason for hiding this comment

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

total nit but black permitting we could do

pt = "series" if isinstance(value, Series) else "frame"

pt = "series"
else:
pt = "frame"

# we are actually a table
if format == "table":
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/io/pytables/test_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import numpy as np

from pandas import DataFrame, Series
import pandas._testing as tm
from pandas.tests.io.pytables.common import ensure_clean_path

from pandas.io.pytables import HDFStore, read_hdf


class TestHDFStoreSubclass:
Copy link
Member

Choose a reason for hiding this comment

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

in pandas._testing we have SubclassedSeries and SubclassedDataFrame. Should we be using those here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing this out!
Changed that.

# GH 33748
def test_supported_for_subclass_dataframe(self):
data = {"a": [1, 2], "b": [3, 4]}
sdf = tm.SubclassedDataFrame(data, dtype=np.intp)

expected = DataFrame(data, dtype=np.intp)

with ensure_clean_path("temp.h5") as path:
sdf.to_hdf(path, "df")
result = read_hdf(path, "df")
tm.assert_frame_equal(result, expected)

with ensure_clean_path("temp.h5") as path:
with HDFStore(path) as store:
store.put("df", sdf)
result = read_hdf(path, "df")
tm.assert_frame_equal(result, expected)

def test_supported_for_subclass_series(self):
data = [1, 2, 3]
sser = tm.SubclassedSeries(data, dtype=np.intp)

expected = Series(data, dtype=np.intp)

with ensure_clean_path("temp.h5") as path:
sser.to_hdf(path, "ser")
result = read_hdf(path, "ser")
tm.assert_series_equal(result, expected)

with ensure_clean_path("temp.h5") as path:
with HDFStore(path) as store:
store.put("ser", sser)
result = read_hdf(path, "ser")
tm.assert_series_equal(result, expected)