Skip to content

BUG: Panel.apply (2d) doesn't work when function returns a scalar of NumPy datatype in 0.16.1 #10332

Closed
@iriyaasagao

Description

@iriyaasagao

Panel.apply (2d) doesn't work when function returns a scalar of NumPy datatype in 0.16.1.

test code is as follows:

import pandas
import numpy

panel = pandas.Panel(numpy.random.randn(5, 5, 5))

panel.apply(lambda df: 0, axis=[1, 2]) # OK

panel.apply(lambda df: 0.0, axis=[1, 2]) # OK

panel.apply(lambda df: numpy.int64(0), axis=[1, 2]) # NG

panel.apply(lambda df: numpy.float64(0.0), axis=[1, 2]) # NG

result is:

yIn [1]: import pandas

In [2]: import numpy

In [3]: panel = pandas.Panel(numpy.random.randn(5, 5, 5))

In [4]: panel.apply(lambda df: 0, axis=[1, 2]) # OK
Out[4]:
0    0
1    0
2    0
3    0
4    0
dtype: int64

In [5]: panel.apply(lambda df: 0.0, axis=[1, 2]) # OK
Out[5]:
0    0
1    0
2    0
3    0
4    0
dtype: float64

In [6]: panel.apply(lambda df: numpy.int64(0), axis=[1, 2]) # NG
---------------------------------------------------------------------------
PandasError                               Traceback (most recent call last)
<ipython-input-6-fa750da7769d> in <module>()
----> 1 panel.apply(lambda df: numpy.int64(0), axis=[1, 2]) # NG

C:\Anaconda3\lib\site-packages\pandas\core\panel.py in apply(self, func, axis, **kwargs)
    967         # 2d-slabs
    968         if isinstance(axis, (tuple,list)) and len(axis) == 2:
--> 969             return self._apply_2d(f, axis=axis)
    970
    971         axis = self._get_axis_number(axis)

C:\Anaconda3\lib\site-packages\pandas\core\panel.py in _apply_2d(self, func, axis)
   1066             results.append((e,obj))
   1067
-> 1068         return self._construct_return_type(dict(results))
   1069
   1070     def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,

C:\Anaconda3\lib\site-packages\pandas\core\panel.py in _construct_return_type(self, result, axes)
   1122
   1123         raise PandasError('invalid _construct_return_type [self->%s] '
-> 1124                           '[result->%s]' % (self, result))
   1125
   1126     def _wrap_result(self, result, axis):

PandasError: invalid _construct_return_type [self-><class 'pandas.core.panel.Panel'>
Dimensions: 5 (items) x 5 (major_axis) x 5 (minor_axis)
Items axis: 0 to 4
Major_axis axis: 0 to 4
Minor_axis axis: 0 to 4] [result->{0: 0, 1: 0, 2: 0, 3: 0, 4: 0}]

In [7]: panel.apply(lambda df: numpy.float64(0.0), axis=[1, 2]) # NG
---------------------------------------------------------------------------
PandasError                               Traceback (most recent call last)
<ipython-input-7-e7ea1dec29c2> in <module>()
----> 1 panel.apply(lambda df: numpy.float64(0.0), axis=[1, 2]) # NG

C:\Anaconda3\lib\site-packages\pandas\core\panel.py in apply(self, func, axis, **kwargs)
    967         # 2d-slabs
    968         if isinstance(axis, (tuple,list)) and len(axis) == 2:
--> 969             return self._apply_2d(f, axis=axis)
    970
    971         axis = self._get_axis_number(axis)

C:\Anaconda3\lib\site-packages\pandas\core\panel.py in _apply_2d(self, func, axis)
   1066             results.append((e,obj))
   1067
-> 1068         return self._construct_return_type(dict(results))
   1069
   1070     def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,

C:\Anaconda3\lib\site-packages\pandas\core\panel.py in _construct_return_type(self, result, axes)
   1122
   1123         raise PandasError('invalid _construct_return_type [self->%s] '
-> 1124                           '[result->%s]' % (self, result))
   1125
   1126     def _wrap_result(self, result, axis):

PandasError: invalid _construct_return_type [self-><class 'pandas.core.panel.Panel'>
Dimensions: 5 (items) x 5 (major_axis) x 5 (minor_axis)
Items axis: 0 to 4
Major_axis axis: 0 to 4
Minor_axis axis: 0 to 4] [result->{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}]

In [8]:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions