Skip to content

Commit 4291973

Browse files
authored
REF: share _reduce (#36561)
1 parent 2ed549d commit 4291973

File tree

7 files changed

+14
-24
lines changed

7 files changed

+14
-24
lines changed

pandas/core/arrays/_mixins.py

+8
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,11 @@ def fillna(self: _T, value=None, method=None, limit=None) -> _T:
227227
else:
228228
new_values = self.copy()
229229
return new_values
230+
231+
def _reduce(self, name: str, skipna: bool = True, **kwargs):
232+
meth = getattr(self, name, None)
233+
if meth:
234+
return meth(skipna=skipna, **kwargs)
235+
else:
236+
msg = f"'{type(self).__name__}' does not implement reduction '{name}'"
237+
raise TypeError(msg)

pandas/core/arrays/categorical.py

-6
Original file line numberDiff line numberDiff line change
@@ -1963,12 +1963,6 @@ def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
19631963
# ------------------------------------------------------------------
19641964
# Reductions
19651965

1966-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
1967-
func = getattr(self, name, None)
1968-
if func is None:
1969-
raise TypeError(f"Categorical cannot perform the operation {name}")
1970-
return func(skipna=skipna, **kwargs)
1971-
19721966
@deprecate_kwarg(old_arg_name="numeric_only", new_arg_name="skipna")
19731967
def min(self, skipna=True, **kwargs):
19741968
"""

pandas/core/arrays/datetimelike.py

-7
Original file line numberDiff line numberDiff line change
@@ -1453,13 +1453,6 @@ def __isub__(self, other):
14531453
# --------------------------------------------------------------
14541454
# Reductions
14551455

1456-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
1457-
op = getattr(self, name, None)
1458-
if op:
1459-
return op(skipna=skipna, **kwargs)
1460-
else:
1461-
return super()._reduce(name, skipna, **kwargs)
1462-
14631456
def min(self, axis=None, skipna=True, *args, **kwargs):
14641457
"""
14651458
Return the minimum value of the Array or minimum along

pandas/core/arrays/numpy_.py

-8
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,6 @@ def _values_for_factorize(self) -> Tuple[np.ndarray, int]:
272272
# ------------------------------------------------------------------------
273273
# Reductions
274274

275-
def _reduce(self, name, skipna=True, **kwargs):
276-
meth = getattr(self, name, None)
277-
if meth:
278-
return meth(skipna=skipna, **kwargs)
279-
else:
280-
msg = f"'{type(self).__name__}' does not implement reduction '{name}'"
281-
raise TypeError(msg)
282-
283275
def any(self, axis=None, out=None, keepdims=False, skipna=True):
284276
nv.validate_any((), dict(out=out, keepdims=keepdims))
285277
return nanops.nanany(self._ndarray, axis=axis, skipna=skipna)

pandas/tests/arrays/categorical/test_operators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def test_numeric_like_ops(self):
353353
# min/max)
354354
s = df["value_group"]
355355
for op in ["kurt", "skew", "var", "std", "mean", "sum", "median"]:
356-
msg = f"Categorical cannot perform the operation {op}"
356+
msg = f"'Categorical' does not implement reduction '{op}'"
357357
with pytest.raises(TypeError, match=msg):
358358
getattr(s, op)(numeric_only=False)
359359

@@ -362,7 +362,7 @@ def test_numeric_like_ops(self):
362362
# numpy ops
363363
s = Series(Categorical([1, 2, 3, 4]))
364364
with pytest.raises(
365-
TypeError, match="Categorical cannot perform the operation sum"
365+
TypeError, match="'Categorical' does not implement reduction 'sum'"
366366
):
367367
np.sum(s)
368368

pandas/tests/arrays/test_datetimelike.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ def test_reduce_invalid(self):
205205
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
206206
arr = self.array_cls(data, freq="D")
207207

208-
with pytest.raises(TypeError, match="cannot perform"):
208+
msg = f"'{type(arr).__name__}' does not implement reduction 'not a method'"
209+
with pytest.raises(TypeError, match=msg):
209210
arr._reduce("not a method")
210211

211212
@pytest.mark.parametrize("method", ["pad", "backfill"])

pandas/tests/reductions/test_reductions.py

+2
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def test_invalid_td64_reductions(self, opname):
351351
[
352352
f"reduction operation '{opname}' not allowed for this dtype",
353353
rf"cannot perform {opname} with type timedelta64\[ns\]",
354+
f"'TimedeltaArray' does not implement reduction '{opname}'",
354355
]
355356
)
356357

@@ -695,6 +696,7 @@ def test_ops_consistency_on_empty(self, method):
695696
[
696697
"operation 'var' not allowed",
697698
r"cannot perform var with type timedelta64\[ns\]",
699+
"'TimedeltaArray' does not implement reduction 'var'",
698700
]
699701
)
700702
with pytest.raises(TypeError, match=msg):

0 commit comments

Comments
 (0)