Skip to content

REF: share _reduce #36561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 0 additions & 6 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'",
]
)

Expand Down Expand Up @@ -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):
Expand Down