Skip to content

Commit 29008f5

Browse files
arnovjreback
authored andcommitted
BUG: Fix passing of numeric_only argument for categorical reduce (pandas-dev#25304)
1 parent b0dac6c commit 29008f5

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-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:`Series.min` and :meth:`Series.max` where ``numeric_only=True`` was ignored when the ``Series`` contained ```Categorical`` data (: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, see GH25303
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

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

963+
def test_min_max_numeric_only(self):
964+
# TODO deprecate numeric_only argument for Categorical and use
965+
# skipna as well, see GH25303
966+
cat = Series(Categorical(
967+
["a", "b", np.nan, "a"], categories=['b', 'a'], ordered=True))
968+
969+
_min = cat.min()
970+
_max = cat.max()
971+
assert np.isnan(_min)
972+
assert _max == "a"
973+
974+
_min = cat.min(numeric_only=True)
975+
_max = cat.max(numeric_only=True)
976+
assert _min == "b"
977+
assert _max == "a"
978+
979+
_min = cat.min(numeric_only=False)
980+
_max = cat.max(numeric_only=False)
981+
assert np.isnan(_min)
982+
assert _max == "a"
983+
963984

964985
class TestSeriesMode(object):
965986
# Note: the name TestSeriesMode indicates these tests

0 commit comments

Comments
 (0)