Pandas有一个nice interface,有助于在HDF5中存储Dataframes和Series等内容:
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()
但是,如果我尝试在同一个文件中保存一些其他常规Python对象,它会抱怨:
my_dictionary = dict()
my_dictionary['a'] = 2 # <--- ERROR
my_dictionary['b'] = [2,3,4]
store['my_dictionary'] = my_dictionary
store.close()
带
TypeError: cannot properly create the storer for: [_TYPE_MAP] [group->/par
ameters (Group) u'',value-><type 'dict'>,table->None,append->False,kwargs-
>{}]
如何在我存储其他Pandas对象的同一HDF5中存储常规Python数据结构?
答案 0 :(得分:12)
以下是食谱中的示例:http://pandas.pydata.org/pandas-docs/stable/cookbook.html#hdfstore
您可以将任意对象存储为节点的属性。我相信有一个64kb的限制(我认为它的节点的总属性数据)。对象被腌制
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}