Skip to content

Commit 8d368fb

Browse files
committed
TST: add failing test for saving subclasses
1 parent 65f0463 commit 8d368fb

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pandas/tests/io/pytables/test_store.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4888,6 +4888,47 @@ def test_unsuppored_hdf_file_error(self, datapath):
48884888
with pytest.raises(ValueError, match=message):
48894889
pd.read_hdf(data_path)
48904890

4891+
def test_supported_for_subclasses_dataframe(self):
4892+
class SubDataFrame(DataFrame):
4893+
@property
4894+
def _constructor(self):
4895+
return SubDataFrame
4896+
4897+
data = {"a": [1, 2], "b": [3, 4]}
4898+
sdf = SubDataFrame(data, dtype=np.intp)
4899+
4900+
expected = np.array([[1, 3], [2, 4]], dtype=np.intp)
4901+
4902+
with ensure_clean_path("temp.h5") as path:
4903+
sdf.to_hdf(path, "df")
4904+
result = read_hdf(path, "df").values
4905+
assert np.array_equal(result, expected)
4906+
4907+
with ensure_clean_path("temp.h5") as path:
4908+
with HDFStore(path) as store:
4909+
store.put("df", sdf)
4910+
result = read_hdf(path, "df").values
4911+
assert np.array_equal(result, expected)
4912+
4913+
def test_supported_for_subclasses_series(self):
4914+
class SubSeries(Series):
4915+
@property
4916+
def _constructor(self):
4917+
return SubSeries
4918+
4919+
sser = SubSeries([1, 2, 3], dtype=np.intp)
4920+
4921+
expected = np.array([1, 2, 3], dtype=np.intp)
4922+
4923+
with ensure_clean_path("temp.h5") as path:
4924+
sser.to_hdf(path, "ser")
4925+
4926+
with ensure_clean_path("temp.h5") as path:
4927+
with HDFStore(path) as store:
4928+
store.put("ser", sser)
4929+
result = read_hdf(path, "ser").values
4930+
assert np.array_equal(result, expected)
4931+
48914932

48924933
@pytest.mark.parametrize("bad_version", [(1, 2), (1,), [], "12", "123"])
48934934
def test_maybe_adjust_name_bad_version_raises(bad_version):

0 commit comments

Comments
 (0)