Skip to content

Sum a panel along multiple dimensions #11857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
srintoul opened this issue Dec 17, 2015 · 3 comments
Closed

Sum a panel along multiple dimensions #11857

srintoul opened this issue Dec 17, 2015 · 3 comments
Labels
API Design Numeric Operations Arithmetic, Comparison, and Logical operations Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@srintoul
Copy link

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.

@max-sixty
Copy link
Contributor

Panel needs a lot of love. This is another one where xray does what you need. Some discussion here on the broader issue: #8906

In your case, you can sum over one axis of the panel, and then again over an axis of the DataFrame.

@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Numeric Operations Arithmetic, Comparison, and Logical operations labels Dec 18, 2015
@jreback
Copy link
Contributor

jreback commented Dec 18, 2015

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

@jreback
Copy link
Contributor

jreback commented Jul 10, 2017

closing as Panels are deprecated

@jreback jreback closed this as completed Jul 10, 2017
@jreback jreback modified the milestones: Someday, won't fix Jul 11, 2017
@TomAugspurger TomAugspurger modified the milestones: won't fix, No action Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Numeric Operations Arithmetic, Comparison, and Logical operations Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

5 participants