我想使用带有日期的MultiIndex作为分层索引类型之一。我还想将DataFrame保存为frame_table,这样我就可以从磁盘中选择子集而不加载整个东西。我目前收到一个错误:TypeError: [date] is not implemented as a table column
我想知道我是否错误地使用了多索引,或者这确实是Pandas的限制。谢谢!
import pandas as pd, numpy, datetime
print pd.__version__ #-> 0.13.0rc1
idx1 = pd.MultiIndex.from_tuples([(datetime.date(2013,12,d), s, t) for d in range(1,3) for s in range(2) for t in range(3)])
df1 = pd.DataFrame(data=numpy.zeros((len(idx1),2)), columns=['a','b'], index=idx1)
with pd.get_store('test1.h5') as f:
f.put('trials',df1) #-> OK
with pd.get_store('test2.h5') as f:
f.put('trials',df1,data_colums=True,format='t') #-> TypeError: [date] is not implemented as a table column
答案 0 :(得分:2)
使用datetime.datetime
,因为这些类型可以有效存储。对于在HDFStore
中存储多索引帧的示例,文档为here。
存储多索引时,必须指定级别的名称(如果您尝试将其存储在ATM中,HDFStore
当前不会发出警告;这将在下一版本中解决)。
In [20]: idx1 = pd.MultiIndex.from_tuples([(datetime.datetime(2013,12,d), s, t) for d in range(1,3) for s in range(2) for t in range(3)],names=['date','s','t'])
In [21]: df1 = pd.DataFrame(data=numpy.zeros((len(idx1),2)), columns=['a','b'], index=idx1)
除非指定table
,否则您需要以put
格式存储Fixed
(append
个商店。
In [22]: df1.to_hdf('test.h5','df',mode='w',format='table')
In [23]: pd.read_hdf('test.h5','df')
Out[23]:
a b
date s t
2013-12-01 0 0 0 0
1 0 0
2 0 0
1 0 0 0
1 0 0
2 0 0
2013-12-02 0 0 0 0
1 0 0
2 0 0
1 0 0 0
1 0 0
2 0 0
[12 rows x 2 columns]
样本选择
In [8]: pd.read_hdf('test.h5','df',where='date=20131202')
Out[8]:
a b
date s t
2013-12-02 0 0 0 0
1 0 0
2 0 0
1 0 0 0
1 0 0
2 0 0
[6 rows x 2 columns]