From 283ff36024f3a92f50959c74f3213dbc6ebe466c Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 22 Sep 2020 15:29:20 -0700 Subject: [PATCH] REF: share _reduce --- pandas/core/arrays/_mixins.py | 8 ++++++++ pandas/core/arrays/categorical.py | 6 ------ pandas/core/arrays/datetimelike.py | 7 ------- pandas/core/arrays/numpy_.py | 8 -------- pandas/tests/arrays/categorical/test_operators.py | 4 ++-- pandas/tests/arrays/test_datetimelike.py | 3 ++- pandas/tests/reductions/test_reductions.py | 2 ++ 7 files changed, 14 insertions(+), 24 deletions(-) diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 808d598558c83..2bf530eb2bad4 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -227,3 +227,11 @@ def fillna(self: _T, value=None, method=None, limit=None) -> _T: else: new_values = self.copy() return new_values + + def _reduce(self, name: str, skipna: bool = True, **kwargs): + meth = getattr(self, name, None) + if meth: + return meth(skipna=skipna, **kwargs) + else: + msg = f"'{type(self).__name__}' does not implement reduction '{name}'" + raise TypeError(msg) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index ef69d6565cfeb..1efe6ac6ae70b 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1986,12 +1986,6 @@ def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]: # ------------------------------------------------------------------ # Reductions - def _reduce(self, name: str, skipna: bool = True, **kwargs): - func = getattr(self, name, None) - if func is None: - raise TypeError(f"Categorical cannot perform the operation {name}") - return func(skipna=skipna, **kwargs) - @deprecate_kwarg(old_arg_name="numeric_only", new_arg_name="skipna") def min(self, skipna=True, **kwargs): """ diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 7051507f9a90e..6752a98345b6a 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1453,13 +1453,6 @@ def __isub__(self, other): # -------------------------------------------------------------- # Reductions - def _reduce(self, name: str, skipna: bool = True, **kwargs): - op = getattr(self, name, None) - if op: - return op(skipna=skipna, **kwargs) - else: - return super()._reduce(name, skipna, **kwargs) - def min(self, axis=None, skipna=True, *args, **kwargs): """ Return the minimum value of the Array or minimum along diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 61076132b24cd..f65b130b396da 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -272,14 +272,6 @@ def _values_for_factorize(self) -> Tuple[np.ndarray, int]: # ------------------------------------------------------------------------ # Reductions - def _reduce(self, name, skipna=True, **kwargs): - meth = getattr(self, name, None) - if meth: - return meth(skipna=skipna, **kwargs) - else: - msg = f"'{type(self).__name__}' does not implement reduction '{name}'" - raise TypeError(msg) - def any(self, axis=None, out=None, keepdims=False, skipna=True): nv.validate_any((), dict(out=out, keepdims=keepdims)) return nanops.nanany(self._ndarray, axis=axis, skipna=skipna) diff --git a/pandas/tests/arrays/categorical/test_operators.py b/pandas/tests/arrays/categorical/test_operators.py index 9d118f1ed8753..34194738bf4ab 100644 --- a/pandas/tests/arrays/categorical/test_operators.py +++ b/pandas/tests/arrays/categorical/test_operators.py @@ -353,7 +353,7 @@ def test_numeric_like_ops(self): # min/max) s = df["value_group"] for op in ["kurt", "skew", "var", "std", "mean", "sum", "median"]: - msg = f"Categorical cannot perform the operation {op}" + msg = f"'Categorical' does not implement reduction '{op}'" with pytest.raises(TypeError, match=msg): getattr(s, op)(numeric_only=False) @@ -362,7 +362,7 @@ def test_numeric_like_ops(self): # numpy ops s = Series(Categorical([1, 2, 3, 4])) with pytest.raises( - TypeError, match="Categorical cannot perform the operation sum" + TypeError, match="'Categorical' does not implement reduction 'sum'" ): np.sum(s) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index f512b168d2795..3f5ab5baa7d69 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -205,7 +205,8 @@ def test_reduce_invalid(self): data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9 arr = self.array_cls(data, freq="D") - with pytest.raises(TypeError, match="cannot perform"): + msg = f"'{type(arr).__name__}' does not implement reduction 'not a method'" + with pytest.raises(TypeError, match=msg): arr._reduce("not a method") @pytest.mark.parametrize("method", ["pad", "backfill"]) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index bbf2d9f1f0784..db7cd54d23a2b 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -351,6 +351,7 @@ def test_invalid_td64_reductions(self, opname): [ f"reduction operation '{opname}' not allowed for this dtype", rf"cannot perform {opname} with type timedelta64\[ns\]", + f"'TimedeltaArray' does not implement reduction '{opname}'", ] ) @@ -695,6 +696,7 @@ def test_ops_consistency_on_empty(self, method): [ "operation 'var' not allowed", r"cannot perform var with type timedelta64\[ns\]", + "'TimedeltaArray' does not implement reduction 'var'", ] ) with pytest.raises(TypeError, match=msg):