Skip to content

Commit 958e36c

Browse files
186: Add accumulate r=andrewgsavage a=andrewgsavage - [x] Closes hgrecco#170 - [x] Executed `pre-commit run --all-files` with no errors - [x] The change is fully covered by automated unit tests - [x] Documented in docs/ as appropriate - [x] Added an entry to the CHANGES file Co-authored-by: Andrew <[email protected]> Co-authored-by: andrewgsavage <[email protected]>
2 parents 44f0f9b + a6b4b84 commit 958e36c

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed

.github/workflows/ci-pint-master.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
matrix:
99
python-version: [3.9, "3.10", "3.11"]
1010
numpy: ["numpy>=1.20.3,<2.0.0"]
11-
pandas: ["pandas==1.5.2", ]
12-
pint: ["pint>=0.20.1"]
11+
pandas: ["pandas==2.0.2", ]
12+
pint: ["pint>=0.21.1"]
1313

1414
runs-on: ubuntu-latest
1515

.github/workflows/ci-pint-pre.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
matrix:
99
python-version: [3.9, "3.10", "3.11"]
1010
numpy: ["numpy>=1.20.3,<2.0.0"]
11-
pandas: ["pandas==1.5.2", ]
12-
pint: ["pint>=0.20.1"]
11+
pandas: ["pandas==2.0.2", ]
12+
pint: ["pint>=0.21.1"]
1313

1414
runs-on: ubuntu-latest
1515

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
matrix:
99
python-version: [3.9, "3.10", "3.11"]
1010
numpy: ["numpy>=1.20.3,<2.0.0"]
11-
pandas: ["pandas==1.5.2","pandas==2.0.2", ]
12-
pint: ["pint==0.21.1", "pint==0.22"]
11+
pandas: ["pandas==2.0.2", ]
12+
pint: ["pint>=0.21.1", "pint==0.22"]
1313

1414
runs-on: ubuntu-latest
1515

CHANGES

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
pint-pandas Changelog
22
=====================
33

4-
0.4 (unreleased)
4+
0.5 (unreleased)
5+
----------------
6+
7+
- Support for pandas 2.0, allowing `.cumsum, .cummax, .cummin` methods for `Series` and `DataFrame`. #186
8+
- Minimum Pint version is 0.21
9+
- Minimum Pandas vesrion is 2.0
10+
- Support for unit registries with `force_ndarray_like = True`. #165
11+
12+
0.4 (2023-05-23)
513
----------------
614

715
- Support for <NA> values in columns with integer magnitudes
816
- Support for magnitudes of any type, such as complex128 or tuples #146
917
- Support for Pint 0.21 #168, #179
1018
- Cast to `numpy.ndarray` in `PintArray._reduce` if needed to use `nanops` functions
11-
- Support for unit registries with `force_ndarray_like = True`. #165
19+
- Minimum Pint version is 0.21
20+
- Minimum Pandas vesrion is 1.6
1221

1322
0.3 (2022-11-14)
1423
----------------

pint_pandas/pint_array.py

+18
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,24 @@ def _reduce(self, name, **kwds):
852852
return self._Q(result, self.units**2)
853853
return self._Q(result, self.units)
854854

855+
def _accumulate(self, name: str, *, skipna: bool = True, **kwds):
856+
if name == "cumprod":
857+
raise TypeError("cumprod not supported for pint arrays")
858+
functions = {
859+
"cummin": np.minimum.accumulate,
860+
"cummax": np.maximum.accumulate,
861+
"cumsum": np.cumsum,
862+
}
863+
864+
if isinstance(self._data, ExtensionArray):
865+
try:
866+
result = self._data._accumulate(name, **kwds)
867+
except NotImplementedError:
868+
result = functions[name](self.numpy_data, **kwds)
869+
print(result)
870+
871+
return self._from_sequence(result, self.units)
872+
855873

856874
PintArray._add_arithmetic_ops()
857875
PintArray._add_comparison_ops()

pint_pandas/testsuite/test_pandas_extensiontests.py

+28
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ def all_boolean_reductions(request):
220220
return request.param
221221

222222

223+
_all_numeric_accumulations = ["cumsum", "cumprod", "cummin", "cummax"]
224+
225+
226+
@pytest.fixture(params=_all_numeric_accumulations)
227+
def all_numeric_accumulations(request):
228+
"""
229+
Fixture for numeric accumulation names
230+
"""
231+
return request.param
232+
233+
223234
@pytest.fixture
224235
def invalid_scalar(data):
225236
"""
@@ -497,3 +508,20 @@ class TestSetitem(base.BaseSetitemTests):
497508
@pytest.mark.parametrize("numeric_dtype", _base_numeric_dtypes, indirect=True)
498509
def test_setitem_scalar_key_sequence_raise(self, data):
499510
base.BaseSetitemTests.test_setitem_scalar_key_sequence_raise(self, data)
511+
512+
513+
class TestAccumulate(base.BaseAccumulateTests):
514+
@pytest.mark.parametrize("skipna", [True, False])
515+
def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna):
516+
pass
517+
518+
def check_accumulate(self, s, op_name, skipna):
519+
if op_name == "cumprod":
520+
with pytest.raises(TypeError):
521+
getattr(s, op_name)(skipna=skipna)
522+
else:
523+
result = getattr(s, op_name)(skipna=skipna)
524+
s_unitless = pd.Series(s.values.data)
525+
expected = getattr(s_unitless, op_name)(skipna=skipna)
526+
expected = pd.Series(expected, dtype=s.dtype)
527+
self.assert_series_equal(result, expected, check_dtype=False)

0 commit comments

Comments
 (0)