|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pandas import DataFrame, Series |
| 4 | +import pandas._testing as tm |
| 5 | +from pandas.tests.io.pytables.common import ensure_clean_path |
| 6 | + |
| 7 | +from pandas.io.pytables import HDFStore, read_hdf |
| 8 | + |
| 9 | + |
| 10 | +class TestHDFStoreSubclass: |
| 11 | + # GH 33748 |
| 12 | + def test_supported_for_subclass_dataframe(self): |
| 13 | + data = {"a": [1, 2], "b": [3, 4]} |
| 14 | + sdf = tm.SubclassedDataFrame(data, dtype=np.intp) |
| 15 | + |
| 16 | + expected = DataFrame(data, dtype=np.intp) |
| 17 | + |
| 18 | + with ensure_clean_path("temp.h5") as path: |
| 19 | + sdf.to_hdf(path, "df") |
| 20 | + result = read_hdf(path, "df") |
| 21 | + tm.assert_frame_equal(result, expected) |
| 22 | + |
| 23 | + with ensure_clean_path("temp.h5") as path: |
| 24 | + with HDFStore(path) as store: |
| 25 | + store.put("df", sdf) |
| 26 | + result = read_hdf(path, "df") |
| 27 | + tm.assert_frame_equal(result, expected) |
| 28 | + |
| 29 | + def test_supported_for_subclass_series(self): |
| 30 | + data = [1, 2, 3] |
| 31 | + sser = tm.SubclassedSeries(data, dtype=np.intp) |
| 32 | + |
| 33 | + expected = Series(data, dtype=np.intp) |
| 34 | + |
| 35 | + with ensure_clean_path("temp.h5") as path: |
| 36 | + sser.to_hdf(path, "ser") |
| 37 | + result = read_hdf(path, "ser") |
| 38 | + tm.assert_series_equal(result, expected) |
| 39 | + |
| 40 | + with ensure_clean_path("temp.h5") as path: |
| 41 | + with HDFStore(path) as store: |
| 42 | + store.put("ser", sser) |
| 43 | + result = read_hdf(path, "ser") |
| 44 | + tm.assert_series_equal(result, expected) |
0 commit comments