Skip to content

Commit 74f5f03

Browse files
author
Arno Veenstra
committed
Make sure numeric_only works for Categorical
1 parent 53281a5 commit 74f5f03

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed Regressions
2525
- Fixed regression in :meth:`DataFrame.apply` causing ``RecursionError`` when ``dict``-like classes were passed as argument. (:issue:`25196`)
2626

2727
- Fixed regression in :meth:`DataFrame.duplicated()`, where empty dataframe was not returning a boolean dtyped Series. (:issue:`25184`)
28+
- Fixed regression in :meth:`Categorical.min` and :meth:`Categorical.max` where ``numeric_only=True`` was ignored (:issue:`25299`)
2829

2930
.. _whatsnew_0242.enhancements:
3031

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ def _reverse_indexer(self):
21722172
return result
21732173

21742174
# reduction ops #
2175-
def _reduce(self, name, axis=0, skipna=True, **kwargs):
2175+
def _reduce(self, name, axis=0, **kwargs):
21762176
func = getattr(self, name, None)
21772177
if func is None:
21782178
msg = 'Categorical cannot perform the operation {op}'

pandas/core/series.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -3678,8 +3678,12 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
36783678
if axis is not None:
36793679
self._get_axis_number(axis)
36803680

3681-
# dispatch to ExtensionArray interface
3682-
if isinstance(delegate, ExtensionArray):
3681+
if isinstance(delegate, Categorical):
3682+
# TODO deprecate numeric_only argument for Categorical and use
3683+
# skipna as well
3684+
return delegate._reduce(name, numeric_only=numeric_only, **kwds)
3685+
elif isinstance(delegate, ExtensionArray):
3686+
# dispatch to ExtensionArray interface
36833687
return delegate._reduce(name, skipna=skipna, **kwds)
36843688
elif is_datetime64_dtype(delegate):
36853689
# use DatetimeIndex implementation to handle skipna correctly

pandas/tests/reductions/test_reductions.py

+12
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,18 @@ def test_min_max(self):
960960
assert np.isnan(_min)
961961
assert _max == 1
962962

963+
cat = Series(Categorical(
964+
["a", "b", np.nan, "a"], categories=['b', 'a'], ordered=True))
965+
_min = cat.min(numeric_only=True)
966+
_max = cat.max(numeric_only=True)
967+
assert _min == "b"
968+
assert _max == "a"
969+
970+
_min = cat.min(numeric_only=False)
971+
_max = cat.max(numeric_only=False)
972+
assert np.isnan(_min)
973+
assert _max == "a"
974+
963975

964976
class TestSeriesMode(object):
965977
# Note: the name TestSeriesMode indicates these tests

0 commit comments

Comments
 (0)