Storing Pandas objects along with regular Python objects in HDF5

主宰稳场 提交于 2019-12-05 16:15:22

问题


Pandas has a nice interface that facilitates storing things like Dataframes and Series in an HDF5:

random_matrix  = np.random.random_integers(0,10, m_size)
my_dataframe =  pd.DataFrame(random_matrix)

store = pd.HDFStore('some_file.h5',complevel=9, complib='bzip2')
store['my_dataframe'] = my_dataframe
store.close()

But if I try to save some other regular Python objects in the same file, it complains:

my_dictionary = dict()
my_dictionary['a'] = 2           # <--- ERROR
my_dictionary['b'] = [2,3,4]

store['my_dictionary'] = my_dictionary
store.close()

with

TypeError: cannot properly create the storer for: [_TYPE_MAP] [group->/par
ameters (Group) u'',value-><type 'dict'>,table->None,append->False,kwargs-
>{}]                                   

How can I store regular Python data structures in the same HDF5 where I store other Pandas objects ?


回答1:


Here's the example from the cookbook: http://pandas.pydata.org/pandas-docs/stable/cookbook.html#hdfstore

You can store arbitrary objects as the attributes of a node. I belive there is a 64kb limit (I think its total attribute data for that node). The objects are pickled

In [1]: df = DataFrame(np.random.randn(8,3))

In [2]: store = HDFStore('test.h5')

In [3]: store['df'] = df

# you can store an arbitrary python object via pickle
In [4]: store.get_storer('df').attrs.my_attribute = dict(A = 10)

In [5]: store.get_storer('df').attrs.my_attribute
{'A': 10}


来源:https://stackoverflow.com/questions/17820071/storing-pandas-objects-along-with-regular-python-objects-in-hdf5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!