gpt4 book ai didi

python - 使用 Pandas Panel 在非唯一轴上分组

转载 作者:行者123 更新时间:2023-11-28 22:52:12 26 4
gpt4 key购买 nike

我有一个带有非唯一 major_axis 的 pandas Panel,我正在尝试使用 groupby 对非唯一行求和,但我收到一条错误消息,指出 major_axis 不可迭代。我搜索了堆栈溢出和留言板,但似乎面板没有数据框那样广泛使用。

这是一个产生错误的例子:

import pandas as pd
import datetime as dt
import dateutil.relativedelta as rd
import numpy as np

items = ['A','B']
minor_axis = ['x','y']

diff = rd.relativedelta(years=1)

major_axis = [dt.date(2013,1,1) + (diff * shift) for shift in xrange(4)] * 2

values = np.random.randn(2,8,2)

data = pd.Panel(data=values, major_axis=major_axis, minor_axis=minor_axis, items=items)

data.groupby(sum, axis='major')

这是堆栈跟踪:

    ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-e30fb9b32fce> in <module>()
----> 1 data.groupby(sum, axis='major')

/home/brendan/python_dev/venv/local/lib/python2.7/site-packages/pandas/core/panel.pyc in groupby(self, function, axis)
1084 from pandas.core.groupby import PanelGroupBy
1085 axis = self._get_axis_number(axis)
-> 1086 return PanelGroupBy(self, function, axis=axis)
1087
1088 def swapaxes(self, axis1='major', axis2='minor', copy=True):

/home/brendan/python_dev/venv/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze)
195 if grouper is None:
196 grouper, exclusions = _get_grouper(obj, keys, axis=axis,
--> 197 level=level, sort=sort)
198
199 self.grouper = grouper

/home/brendan/python_dev/venv/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in _get_grouper(obj, key, axis, level, sort)
1323 raise AssertionError(errmsg)
1324
-> 1325 ping = Grouping(group_axis, gpr, name=name, level=level, sort=sort)
1326 groupings.append(ping)
1327

/home/brendan/python_dev/venv/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in __init__(self, index, grouper, name, level, sort)
1197 # no level passed
1198 if not isinstance(self.grouper, np.ndarray):
-> 1199 self.grouper = self.index.map(self.grouper)
1200 if not (hasattr(self.grouper,"__len__") and \
1201 len(self.grouper) == len(self.index)):

/home/brendan/python_dev/venv/local/lib/python2.7/site-packages/pandas/core/index.pyc in map(self, mapper)
856
857 def map(self, mapper):
--> 858 return self._arrmap(self.values, mapper)
859
860 def isin(self, values):

/home/brendan/python_dev/venv/local/lib/python2.7/site-packages/pandas/algos.so in pandas.algos.arrmap_object (pandas/algos.c:62269)()

TypeError: 'datetime.date' object is not iterable

关于如何处理这种情况有什么想法吗?

非常感谢,

布兰登

最佳答案

在0.12你可以试试

>>> data.groupby(np.sum, axis='major')
<pandas.core.groupby.PanelGroupBy object at 0x1a2ba50>

关于python - 使用 Pandas Panel 在非唯一轴上分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20752322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com