BUG: to_hdf and HDFStore raise KeyError for DataFrame subclasses · Issue #33748 · pandas-dev/pandas (original) (raw)
- [x ] I have checked that this issue has not already been reported.
- [x ] I have confirmed this bug exists on the latest version of pandas.
Code Sample, a copy-pastable example
import pandas as pd class SubDataFrame(pd.DataFrame): @property def _constructor(self): return SubDataFrame
fails with KeyError
sdf = SubDataFrame({'a':[1,2], 'b':[3,4]}) sdf.to_hdf('test.h5', 'test')
with pd.HDFStore('test.h5') as store: store.put('test', sdf)
Problem description
to_hdf()
and HDFStore.put()
fail for DataFrame subclasses.
This happens because in pandas/io/pytables.py
in _create_storer
line 1578 (or thereabouts), the _TYPE_MAP
is accessed by type()
, whereas upon _create_storer
entry, the check is done using isinstance()
.
Expected Output
The check upon entry of _create_storer
should at least be consistent with the way _TYPE_MAP
is accessed. So if the choice is not to support writing DataFrame subclasses to HDF, instead of a KeyError
, a TypeError("value must be None, Series, or DataFrame")
should be raised.
But ideally, storing subclasses of DataFrame to HDF should be supported.
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.6.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.14.111-1.el7.centos.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200325
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
IPython : 7.10.2
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : 0.3.2
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : 0.4.0
scipy : 1.2.1
sqlalchemy : 1.3.11
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.46.0