Skip to content

Commit d0c4bda

Browse files
committed
add whatsnew + test + changed logic
1 parent 85c4455 commit d0c4bda

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ Performance Improvements
279279

280280
- Improved performance of ``pd.wide_to_long()`` (:issue:`14779`)
281281
- Increased performance of ``pd.factorize()`` by releasing the GIL with ``object`` dtype when inferred as strings (:issue:`14859`)
282-
282+
- Increased performance of ``groupby().cummin()`` and ``groupby().cummax()`` (:issue:`15048`)
283283

284284

285285
.. _whatsnew_0200.bug_fixes:

pandas/src/algos_groupby_helper.pxi.in

+3-3
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,10 @@ def group_cummin(numeric[:, :] out,
695695
if val == val:
696696
if j == 0:
697697
min_val = val
698-
elif val < min_val:
698+
elif val < accum[lab, j]:
699699
min_val = val
700700
accum[lab, j] = min_val
701-
out[i, j] = accum[lab, j]
701+
out[i, j] = min_val
702702

703703

704704
@cython.boundscheck(False)
@@ -729,7 +729,7 @@ def group_cummax(numeric[:, :] out,
729729
if val == val:
730730
if j == 0:
731731
max_val = val
732-
elif val > max_val:
732+
elif val > accum[lab, j]:
733733
max_val = val
734734
accum[lab, j] = max_val
735735
out[i, j] = accum[lab, j]

pandas/tests/groupby/test_groupby.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -4977,8 +4977,6 @@ def test_groupby_whitelist(self):
49774977
'max',
49784978
'head',
49794979
'tail',
4980-
'cumsum',
4981-
'cumprod',
49824980
'cumcount',
49834981
'resample',
49844982
'describe',
@@ -5016,8 +5014,6 @@ def test_groupby_whitelist(self):
50165014
'max',
50175015
'head',
50185016
'tail',
5019-
'cumsum',
5020-
'cumprod',
50215017
'cumcount',
50225018
'resample',
50235019
'describe',
@@ -5773,6 +5769,17 @@ def test_agg_over_numpy_arrays(self):
57735769

57745770
assert_frame_equal(result, expected)
57755771

5772+
def test_cummin_cummax(self):
5773+
df = pd.DataFrame({'A': np.random.randint(0, 5, size=100),
5774+
'B': np.random.randn(100)})
5775+
result = df.groupby('A').cummin()
5776+
expected = df.groupby('A').apply(np.minimum.accumulate)['B'].to_frame()
5777+
tm.assert_frame_equal(result, expected)
5778+
5779+
result = df.groupby('A').cummax()
5780+
expected = df.groupby('A').apply(np.maximum.accumulate)['B'].to_frame()
5781+
tm.assert_frame_equal(result, expected)
5782+
57765783

57775784
def assert_fp_equal(a, b):
57785785
assert (np.abs(a - b) < 1e-12).all()

0 commit comments

Comments
 (0)