在HDF5中将Pandas对象与常规Python对象一起存储 [英] Storing Pandas objects along with regular Python objects in HDF5

查看:105
本文介绍了在HDF5中将Pandas对象与常规Python对象一起存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pandas具有一个漂亮的界面,该界面有助于在HDF5中存储数据框和系列等内容:

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()

但是,如果我尝试将其他一些常规Python对象保存在同一文件中,则会抱怨:

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()

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数据结构?

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

推荐答案

以下是食谱中的示例:

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

您可以将任意对象存储为节点的属性.我相信这里有一个64kb的限制(我认为该节点的总属性数据).物件被腌制

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}

这篇关于在HDF5中将Pandas对象与常规Python对象一起存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆