Skip to content

Commit

Permalink
Merge #186
Browse files Browse the repository at this point in the history
186: Add accumulate r=andrewgsavage a=andrewgsavage

- [x] Closes #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 <andrewgsavage@gmail.com>
Co-authored-by: andrewgsavage <andrewgsavage@gmail.com>
  • Loading branch information
bors[bot] and andrewgsavage committed Jun 24, 2023
2 parents 44f0f9b + a6b4b84 commit 958e36c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-pint-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
matrix:
python-version: [3.9, "3.10", "3.11"]
numpy: ["numpy>=1.20.3,<2.0.0"]
pandas: ["pandas==1.5.2", ]
pint: ["pint>=0.20.1"]
pandas: ["pandas==2.0.2", ]
pint: ["pint>=0.21.1"]

runs-on: ubuntu-latest

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-pint-pre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
matrix:
python-version: [3.9, "3.10", "3.11"]
numpy: ["numpy>=1.20.3,<2.0.0"]
pandas: ["pandas==1.5.2", ]
pint: ["pint>=0.20.1"]
pandas: ["pandas==2.0.2", ]
pint: ["pint>=0.21.1"]

runs-on: ubuntu-latest

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
matrix:
python-version: [3.9, "3.10", "3.11"]
numpy: ["numpy>=1.20.3,<2.0.0"]
pandas: ["pandas==1.5.2","pandas==2.0.2", ]
pint: ["pint==0.21.1", "pint==0.22"]
pandas: ["pandas==2.0.2", ]
pint: ["pint>=0.21.1", "pint==0.22"]

runs-on: ubuntu-latest

Expand Down
13 changes: 11 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
pint-pandas Changelog
=====================

0.4 (unreleased)
0.5 (unreleased)
----------------

- Support for pandas 2.0, allowing `.cumsum, .cummax, .cummin` methods for `Series` and `DataFrame`. #186
- Minimum Pint version is 0.21
- Minimum Pandas vesrion is 2.0
- Support for unit registries with `force_ndarray_like = True`. #165

0.4 (2023-05-23)
----------------

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

0.3 (2022-11-14)
----------------
Expand Down
18 changes: 18 additions & 0 deletions pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,24 @@ def _reduce(self, name, **kwds):
return self._Q(result, self.units**2)
return self._Q(result, self.units)

def _accumulate(self, name: str, *, skipna: bool = True, **kwds):
if name == "cumprod":
raise TypeError("cumprod not supported for pint arrays")
functions = {
"cummin": np.minimum.accumulate,
"cummax": np.maximum.accumulate,
"cumsum": np.cumsum,
}

if isinstance(self._data, ExtensionArray):
try:
result = self._data._accumulate(name, **kwds)
except NotImplementedError:
result = functions[name](self.numpy_data, **kwds)
print(result)

return self._from_sequence(result, self.units)


PintArray._add_arithmetic_ops()
PintArray._add_comparison_ops()
Expand Down
28 changes: 28 additions & 0 deletions pint_pandas/testsuite/test_pandas_extensiontests.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ def all_boolean_reductions(request):
return request.param


_all_numeric_accumulations = ["cumsum", "cumprod", "cummin", "cummax"]


@pytest.fixture(params=_all_numeric_accumulations)
def all_numeric_accumulations(request):
"""
Fixture for numeric accumulation names
"""
return request.param


@pytest.fixture
def invalid_scalar(data):
"""
Expand Down Expand Up @@ -497,3 +508,20 @@ class TestSetitem(base.BaseSetitemTests):
@pytest.mark.parametrize("numeric_dtype", _base_numeric_dtypes, indirect=True)
def test_setitem_scalar_key_sequence_raise(self, data):
base.BaseSetitemTests.test_setitem_scalar_key_sequence_raise(self, data)


class TestAccumulate(base.BaseAccumulateTests):
@pytest.mark.parametrize("skipna", [True, False])
def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna):
pass

def check_accumulate(self, s, op_name, skipna):
if op_name == "cumprod":
with pytest.raises(TypeError):
getattr(s, op_name)(skipna=skipna)
else:
result = getattr(s, op_name)(skipna=skipna)
s_unitless = pd.Series(s.values.data)
expected = getattr(s_unitless, op_name)(skipna=skipna)
expected = pd.Series(expected, dtype=s.dtype)
self.assert_series_equal(result, expected, check_dtype=False)

0 comments on commit 958e36c

Please sign in to comment.