Skip to content

Commit 3f20727

Browse files
dsaxtonKevin D Smith
authored and
Kevin D Smith
committed
ENH: Implement IntegerArray reductions (pandas-dev#36761)
1 parent b688f17 commit 3f20727

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Other enhancements
168168
- ``Styler`` now allows direct CSS class name addition to individual data cells (:issue:`36159`)
169169
- :meth:`Rolling.mean()` and :meth:`Rolling.sum()` use Kahan summation to calculate the mean to avoid numerical problems (:issue:`10319`, :issue:`11645`, :issue:`13254`, :issue:`32761`, :issue:`36031`)
170170
- :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with datetimelike dtypes will now try to cast string arguments (listlike and scalar) to the matching datetimelike type (:issue:`36346`)
171+
- Added methods :meth:`IntegerArray.prod`, :meth:`IntegerArray.min`, and :meth:`IntegerArray.max` (:issue:`33790`)
171172

172173
.. _whatsnew_120.api_breaking.python:
173174

pandas/core/arrays/integer.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from pandas.core.dtypes.missing import isna
2626

2727
from pandas.core import ops
28-
from pandas.core.array_algos import masked_reductions
2928
from pandas.core.ops import invalid_comparison
3029
from pandas.core.ops.common import unpack_zerodim_and_defer
3130
from pandas.core.tools.numeric import to_numeric
@@ -550,10 +549,19 @@ def cmp_method(self, other):
550549

551550
def sum(self, skipna=True, min_count=0, **kwargs):
552551
nv.validate_sum((), kwargs)
553-
result = masked_reductions.sum(
554-
values=self._data, mask=self._mask, skipna=skipna, min_count=min_count
555-
)
556-
return result
552+
return super()._reduce("sum", skipna=skipna, min_count=min_count)
553+
554+
def prod(self, skipna=True, min_count=0, **kwargs):
555+
nv.validate_prod((), kwargs)
556+
return super()._reduce("prod", skipna=skipna, min_count=min_count)
557+
558+
def min(self, skipna=True, **kwargs):
559+
nv.validate_min((), kwargs)
560+
return super()._reduce("min", skipna=skipna)
561+
562+
def max(self, skipna=True, **kwargs):
563+
nv.validate_max((), kwargs)
564+
return super()._reduce("max", skipna=skipna)
557565

558566
def _maybe_mask_result(self, result, mask, other, op_name: str):
559567
"""

pandas/tests/arrays/integer/test_function.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,41 @@ def test_value_counts_empty():
115115

116116
@pytest.mark.parametrize("skipna", [True, False])
117117
@pytest.mark.parametrize("min_count", [0, 4])
118-
def test_integer_array_sum(skipna, min_count):
119-
arr = pd.array([1, 2, 3, None], dtype="Int64")
118+
def test_integer_array_sum(skipna, min_count, any_nullable_int_dtype):
119+
dtype = any_nullable_int_dtype
120+
arr = pd.array([1, 2, 3, None], dtype=dtype)
120121
result = arr.sum(skipna=skipna, min_count=min_count)
121122
if skipna and min_count == 0:
122123
assert result == 6
123124
else:
124125
assert result is pd.NA
125126

126127

128+
@pytest.mark.parametrize("skipna", [True, False])
129+
@pytest.mark.parametrize("method", ["min", "max"])
130+
def test_integer_array_min_max(skipna, method, any_nullable_int_dtype):
131+
dtype = any_nullable_int_dtype
132+
arr = pd.array([0, 1, None], dtype=dtype)
133+
func = getattr(arr, method)
134+
result = func(skipna=skipna)
135+
if skipna:
136+
assert result == (0 if method == "min" else 1)
137+
else:
138+
assert result is pd.NA
139+
140+
141+
@pytest.mark.parametrize("skipna", [True, False])
142+
@pytest.mark.parametrize("min_count", [0, 9])
143+
def test_integer_array_prod(skipna, min_count, any_nullable_int_dtype):
144+
dtype = any_nullable_int_dtype
145+
arr = pd.array([1, 2, None], dtype=dtype)
146+
result = arr.prod(skipna=skipna, min_count=min_count)
147+
if skipna and min_count == 0:
148+
assert result == 2
149+
else:
150+
assert result is pd.NA
151+
152+
127153
@pytest.mark.parametrize(
128154
"values, expected", [([1, 2, 3], 6), ([1, 2, 3, None], 6), ([None], 0)]
129155
)

0 commit comments

Comments
 (0)