Skip to content

Commit 6faa27e

Browse files
committed
review comments
1 parent d592f2a commit 6faa27e

File tree

7 files changed

+21
-14
lines changed

7 files changed

+21
-14
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ update the ``ExtensionDtype._metadata`` tuple to match the signature of your
556556
- Slicing a single row of a ``DataFrame`` with multiple ExtensionArrays of the same type now preserves the dtype, rather than coercing to object (:issue:`22784`)
557557
- Added :meth:`pandas.api.types.register_extension_dtype` to register an extension type with pandas (:issue:`22664`)
558558
- Updated the ``.type`` attribute for ``PeriodDtype``, ``DatetimeTZDtype``, and ``IntervalDtype`` to be instances of the dtype (``Period``, ``Timestamp``, and ``Interval`` respectively) (:issue:`22938`)
559+
- Support for reduction operations such as ``sum``, ``mean`` via opt-in base class method override (:issue:`22762`)
559560

560561
.. _whatsnew_0240.api.incompatibilities:
561562

pandas/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def all_arithmetic_operators(request):
132132

133133

134134
_all_numeric_reductions = ['sum', 'max', 'min',
135-
'mean', 'prod', 'std', 'var', 'median']
135+
'mean', 'prod', 'std', 'var', 'median',
136+
'kurt', 'skew']
136137

137138

138139
@pytest.fixture(params=_all_numeric_reductions)

pandas/core/arrays/base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,17 +675,20 @@ def _ndarray_values(self):
675675
return np.array(self)
676676

677677
def _reduce(self, name, skipna=True, **kwargs):
678-
"""Return a scalar result of performing the op
678+
"""Return a scalar result of performing the reduction operation.
679679
680680
Parameters
681681
----------
682682
name : str
683-
name of the function
683+
name of the function, support values are:
684+
{any, all, min, max, sum, mean, median, prod,
685+
std, var, sem, kurt, skew}
684686
axis : int, default 0
685687
axis over which to apply, defined as 0 currently
686688
skipna : bool, default True
687689
if True, skip NaN values
688690
kwargs : dict
691+
ddof is the only supported kwarg
689692
690693
Returns
691694
-------
@@ -695,7 +698,8 @@ def _reduce(self, name, skipna=True, **kwargs):
695698
------
696699
TypeError : subclass does not define reductions
697700
"""
698-
raise TypeError
701+
raise TypeError("cannot perform {name} with type {dtype}".format(
702+
name=name, dtype=self.dtype))
699703

700704

701705
class ExtensionOpsMixin(object):

pandas/core/arrays/integer.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def cmp_method(self, other):
530530
name = '__{name}__'.format(name=op.__name__)
531531
return set_function_name(cmp_method, name, cls)
532532

533-
def _reduce(self, name, axis=0, skipna=True, **kwargs):
533+
def _reduce(self, name, skipna=True, **kwargs):
534534
data = self._data
535535
mask = self._mask
536536

@@ -540,17 +540,15 @@ def _reduce(self, name, axis=0, skipna=True, **kwargs):
540540
data[mask] = self._na_value
541541

542542
op = getattr(nanops, 'nan' + name)
543-
result = op(data, axis=axis, skipna=skipna, mask=mask)
543+
result = op(data, axis=0, skipna=skipna, mask=mask)
544544

545-
# if we have a boolean op, provide coercion back to a bool
546-
# type if possible
545+
# if we have a boolean op, don't coerce
547546
if name in ['any', 'all']:
548-
if is_integer(result) or is_float(result):
549-
result = bool(int(result))
547+
pass
550548

551549
# if we have a numeric op, provide coercion back to an integer
552550
# type if possible
553-
elif not isna(result):
551+
elif notna(result):
554552
int_result = int(result)
555553
if int_result == result:
556554
result = int_result

pandas/core/series.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3408,6 +3408,9 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
34083408
with np.errstate(all='ignore'):
34093409
return op(delegate, skipna=skipna, **kwds)
34103410

3411+
# TODO(EA) dispatch to Index
3412+
# remove once all internals extension types are
3413+
# moved to ExtensionArrays
34113414
return delegate._reduce(op=op, name=name, axis=axis, skipna=skipna,
34123415
numeric_only=numeric_only,
34133416
filter_type=filter_type, **kwds)

pandas/tests/extension/decimal/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _na_value(self):
134134
def _concat_same_type(cls, to_concat):
135135
return cls(np.concatenate([x._data for x in to_concat]))
136136

137-
def _reduce(self, name, axis=0, skipna=True, **kwargs):
137+
def _reduce(self, name, skipna=True, **kwargs):
138138

139139
if skipna:
140140
raise NotImplementedError("decimal does not support skipna=True")
@@ -144,7 +144,7 @@ def _reduce(self, name, axis=0, skipna=True, **kwargs):
144144
except AttributeError:
145145
raise NotImplementedError("decimal does not support "
146146
"the {} operation".format(name))
147-
return op(axis=axis)
147+
return op(axis=0)
148148

149149

150150
def to_decimal(values, context=None):

pandas/tests/extension/decimal/test_decimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Reduce:
135135

136136
def check_reduce(self, s, op_name, skipna):
137137

138-
if skipna or op_name in ['median']:
138+
if skipna or op_name in ['median', 'skew', 'kurt']:
139139
with pytest.raises(NotImplementedError):
140140
getattr(s, op_name)(skipna=skipna)
141141

0 commit comments

Comments
 (0)