numpy's sum()
function allows specifying a tuple of axis values for higher-dimensional arrays, but when it is called this way on a Pandas Panel
or Panel4D
it raises a ValueError:
>>> import numpy as np
>>> import pandas as pd
>>> a = np.random.randn(3, 3, 3)
>>> a.sum(axis=(0, 1))
array([-1.91974326, 1.49781967, -1.29698099])
>>> pan = pd.Panel(a)
>>> pan.sum(axis=(0, 1)) # should return the same vector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas/core/generic.py", line 4554, in stat_func
skipna=skipna, numeric_only=numeric_only)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas/core/panel.py", line 1084, in _reduce
axis_name = self._get_axis_name(axis)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas/core/generic.py", line 327, in _get_axis_name
.format(axis, type(self)))
ValueError: No axis named (0, 2) for object type <class 'pandas.core.panel.Panel'>
>>> np.sum(pan, axis=(0, 1)) # try it another way
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/fromnumeric.py", line 1828, in sum
return sum(axis=axis, dtype=dtype, out=out)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas/core/generic.py", line 4554, in stat_func
skipna=skipna, numeric_only=numeric_only)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas/core/panel.py", line 1084, in _reduce
axis_name = self._get_axis_name(axis)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas/core/generic.py", line 327, in _get_axis_name
.format(axis, type(self)))
ValueError: No axis named (0, 1) for object type <class 'pandas.core.panel.Panel'>
Right now the workaround is to call np.asarray()
on the Pandas object first and then convert it back to Series
(or DataFrame
, I suppose) once the result is returned, being sure to apply the correct index to the resulting object.
Comment From: max-sixty
Panel needs a lot of love. This is another one where xray does what you need. Some discussion here on the broader issue: https://github.com/pydata/pandas/issues/8906
In your case, you can sum over one axis of the panel, and then again over an axis of the DataFrame.
Comment From: jreback
I suppose, though this only applies to a fixed dtype Panel
, so not really sure how useful this would be as compared to doing a reduction using the .values
and/or a 2-step reduction (in fact that's all that would be happening internally).
Comment From: jreback
closing as Panels are deprecated