可以保存Panel4d和PanelND对象吗?

时间:2013-06-23 14:23:12

标签: python pandas

我现在意识到实验性的PanelND对象能够很好地满足我的需求,除非看起来我无法保存它们:

p4d = pd.Panel4D(np.random.randn(2, 2, 5, 4),
    labels=['Label1','Label2'],
    items=['Item1', 'Item2'],
    major_axis=pd.date_range('1/1/2000', periods=5),
    minor_axis=['A', 'B', 'C', 'D'])
p4d.save('p4d')
...
PicklingError: Can't pickle <class 'pandas.core.panelnd.Panel4D'>: attribute lookup pandas.core.panelnd.Panel4D failed

如果我尝试将其写入HDFStore,我会得到:

TypeError: cannot properly create the storer for: [_STORER_MAP] [group->/p4d (Group) u'',value-><class 'pandas.core.panelnd.Panel4D'>,table->None,append->False,kwargs->{}]

除了保存单个DataFrame并将它们拼接在一起之外,我如何坚持更高维度的对象?

修改:我看到store.append()适用于Panel4D但save()没有,store.append()也不适用于示例Panel5D。我真的高于4D,所以问题仍然存在。

编辑:更多信息:

我正在尝试在跨越维度的嵌套循环内创建任意尺寸的面板,然后能够再次任意切割该数据,以便我可以处理它(整理,绘图,优化)

在(粗略)代码中:

for a in range(1,10):
    panel4ddict = {}
    for b in range(101, 150):
    paneldict = {}
        for c in range(500, 501):
            df = MakeDataFrame(a, b, c) # returns processed df
            paneldict[c] = df
        p3d = Panel(paneldict)
        panel4ddict[b] = p3d
    p4d = Panel4D(panel4ddict)
    panel5ddict[a] = p4d
panel5d = Panel5D(panel5ddict)

sliced = panel5d[:,3,5:6]
# and then do some plotting of my sliced DF

1 个答案:

答案 0 :(得分:1)

这是一种存储Panel5D的方法。基本上,您将每个Panel4D作为一个单独的组存储在商店中,然后在回读时重建。

请注意,最好将其存储为具有多级(3级或更多级)的DataFrame,其实际上包含与Panel5D相同的信息,但是长时间展开。

 In [1]: from pandas.core import panelnd, panel4d
        from pandas.utils import testing as tm 

In [2]: Panel5D = panelnd.create_nd_panel_factory(
   ...:     klass_name='Panel5D',
   ...:     axis_orders=['cool', 'labels', 'items', 'major_axis',
   ...:                  'minor_axis'],
   ...:     axis_slices={'labels': 'labels', 'items': 'items',
   ...:                  'major_axis': 'major_axis',
   ...:                  'minor_axis': 'minor_axis'},
   ...:     slicer=panel4d.Panel4D,
   ...:     axis_aliases={'major': 'major_axis', 'minor': 'minor_axis'},
   ...:     stat_axis=2)

In [4]: p4d = panel4d.Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel()))

In [5]: p5d = Panel5D(dict(C1 = p4d, C2 = p4d+1))

In [6]: p5d
Out[6]: 
<class 'pandas.core.panelnd.Panel5D'>
Dimensions: 2 (cool) x 2 (labels) x 3 (items) x 30 (major_axis) x 4 (minor_axis)
Cool axis: C1 to C2
Labels axis: L1 to L2
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
Minor_axis axis: A to D

In [7]: store = pd.HDFStore('test.h5',mode='w')

In [9]: for x in p5d.cool:
    store.append(x,p5d[x])
   ...:     

In [10]: store
Out[10]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/C1            wide_table   (typ->appendable,nrows->360,ncols->2,indexers->[items,major_axis,minor_axis])
/C2            wide_table   (typ->appendable,nrows->360,ncols->2,indexers->[items,major_axis,minor_axis])

In [11]: store.close()