Skip to content

Commit 77a0f19

Browse files
authored
ENH: Implement IntegerArray.sum (#33538)
1 parent f49269f commit 77a0f19

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ Other
662662
- Bug in :meth:`Series.map` not raising on invalid ``na_action`` (:issue:`32815`)
663663
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
664664
- Bug in :meth:`DataFrame.plot.scatter` caused an error when plotting variable marker sizes (:issue:`32904`)
665+
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
665666

666667
.. ---------------------------------------------------------------------------
667668

pandas/compat/numpy/function.py

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def validate_cum_func_with_skipna(skipna, args, kwargs, name):
252252
STAT_FUNC_DEFAULTS["out"] = None
253253

254254
PROD_DEFAULTS = SUM_DEFAULTS = STAT_FUNC_DEFAULTS.copy()
255+
SUM_DEFAULTS["axis"] = None
255256
SUM_DEFAULTS["keepdims"] = False
256257
SUM_DEFAULTS["initial"] = None
257258

pandas/core/arrays/integer.py

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pandas._libs import lib, missing as libmissing
88
from pandas._typing import ArrayLike
99
from pandas.compat import set_function_name
10+
from pandas.compat.numpy import function as nv
1011
from pandas.util._decorators import cache_readonly
1112

1213
from pandas.core.dtypes.base import ExtensionDtype
@@ -573,6 +574,13 @@ def _reduce(self, name: str, skipna: bool = True, **kwargs):
573574

574575
return result
575576

577+
def sum(self, skipna=True, min_count=0, **kwargs):
578+
nv.validate_sum((), kwargs)
579+
result = masked_reductions.sum(
580+
values=self._data, mask=self._mask, skipna=skipna, min_count=min_count
581+
)
582+
return result
583+
576584
def _maybe_mask_result(self, result, mask, other, op_name: str):
577585
"""
578586
Parameters

pandas/tests/arrays/integer/test_function.py

+20
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ def test_value_counts_empty():
113113
tm.assert_series_equal(result, expected)
114114

115115

116+
@pytest.mark.parametrize("skipna", [True, False])
117+
@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")
120+
result = arr.sum(skipna=skipna, min_count=min_count)
121+
if skipna and min_count == 0:
122+
assert result == 6
123+
else:
124+
assert result is pd.NA
125+
126+
127+
@pytest.mark.parametrize(
128+
"values, expected", [([1, 2, 3], 6), ([1, 2, 3, None], 6), ([None], 0)]
129+
)
130+
def test_integer_array_numpy_sum(values, expected):
131+
arr = pd.array(values, dtype="Int64")
132+
result = np.sum(arr)
133+
assert result == expected
134+
135+
116136
# TODO(jreback) - these need testing / are broken
117137

118138
# shift

0 commit comments

Comments
 (0)