Skip to content

Commit 436547d

Browse files
author
Arno Veenstra
committed
Make sure numeric_only works for Categorical
1 parent b144f66 commit 436547d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Diff for: 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}'

Diff for: pandas/core/series.py

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

3677-
# dispatch to ExtensionArray interface
3678-
if isinstance(delegate, ExtensionArray):
3677+
if isinstance(delegate, Categorical):
3678+
# TODO deprecate numeric_only argument for Categorical and use
3679+
# skipna as well
3680+
return delegate._reduce(name, numeric_only=numeric_only, **kwds)
3681+
elif isinstance(delegate, ExtensionArray):
3682+
# dispatch to ExtensionArray interface
36793683
return delegate._reduce(name, skipna=skipna, **kwds)
36803684
elif is_datetime64_dtype(delegate):
36813685
# use DatetimeIndex implementation to handle skipna correctly

Diff for: 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", "c", "a"], categories=["b", "c", "d"], ordered=True))
965+
_min = cat.min(numeric_only=True)
966+
_max = cat.max(numeric_only=True)
967+
assert _min == "b"
968+
assert _max == "c"
969+
970+
_min = cat.min(numeric_only=False)
971+
_max = cat.max(numeric_only=False)
972+
assert np.isnan(_min)
973+
assert _max == "c"
974+
963975

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

0 commit comments

Comments
 (0)