Skip to content

Commit

Permalink
simplify EA._reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Sep 23, 2018
1 parent 6d40678 commit 8103b8c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
8 changes: 1 addition & 7 deletions pandas/core/arrays/base.py
Expand Up @@ -712,23 +712,17 @@ def _add_comparison_ops(cls):
cls.__le__ = cls._create_comparison_method(operator.le)
cls.__ge__ = cls._create_comparison_method(operator.ge)

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwargs):
def _reduce(self, name, skipna=True, **kwargs):
"""Return a scalar result of performing the op
Parameters
----------
op : callable
function to apply to the array
name : str
name of the function
axis : int, default 0
axis over which to apply, defined as 0 currently
skipna : bool, default True
if True, skip NaN values
numeric_only : bool, optional
if True, only perform numeric ops
filter_type : str, optional
kwargs : dict
Returns
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/integer.py
Expand Up @@ -8,6 +8,7 @@
from pandas.compat import u, range, string_types
from pandas.compat import set_function_name

from pandas.core import nanops
from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -529,8 +530,7 @@ def cmp_method(self, other):
name = '__{name}__'.format(name=op.__name__)
return set_function_name(cmp_method, name, cls)

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
def _reduce(self, name, axis=0, skipna=True, **kwargs):
data = self._data
mask = self._mask

Expand All @@ -539,6 +539,7 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
data = self._data.astype('float64')
data[mask] = self._na_value

op = getattr(nanops, 'nan' + name)
result = op(data, axis=axis, skipna=skipna)

# if we have a boolean op, provide coercion back to a bool
Expand Down
14 changes: 10 additions & 4 deletions pandas/core/series.py
Expand Up @@ -3342,10 +3342,16 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
"""
delegate = self._values
if isinstance(delegate, np.ndarray):
# Validate that 'axis' is consistent with Series's single axis.
if axis is not None:
self._get_axis_number(axis)

if axis is not None:
self._get_axis_number(axis)

# dispatch to ExtensionArray interface
if isinstance(delegate, ExtensionArray):
return delegate._reduce(name, skipna=skipna, **kwds)

# dispatch to numpy arrays
elif isinstance(delegate, np.ndarray):
if numeric_only:
raise NotImplementedError('Series.{0} does not implement '
'numeric_only.'.format(name))
Expand Down
32 changes: 9 additions & 23 deletions pandas/tests/extension/decimal/array.py
Expand Up @@ -137,30 +137,16 @@ def _na_value(self):
def _concat_same_type(cls, to_concat):
return cls(np.concatenate([x._data for x in to_concat]))

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
"""Return a scalar result of performing the op
Parameters
----------
op : callable
function to apply to the array
name : str
name of the function
axis : int, default 0
axis over which to apply, defined as 0 currently
skipna : bool, default True
if True, skip NaN values
numeric_only : bool, optional
if True, only perform numeric ops
filter_type : str, optional
kwds : dict
def _reduce(self, name, axis=0, skipna=True, **kwargs):

Returns
-------
scalar
"""
return op(self.data, axis=axis, skipna=skipna)
# select the nan* ops
if skipna:
op = getattr(self.data, 'nan' + name)

# don't skip nans
else:
op = getattr(self.data, name)
return op(axis=axis)


DecimalArray._add_arithmetic_ops()
Expand Down

0 comments on commit 8103b8c

Please sign in to comment.