Skip to content

Commit 6895f74

Browse files
authored
BUG: Fix issue with negative labels in group_cumsum (#58984)
* BUG: Fix issue with negative labels in group_cumsum function * Remove blank line * Revert remove blank line * Add test * Add what's new * typo
1 parent 5435b1d commit 6895f74

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ Groupby/resample/rolling
571571
- Bug in :meth:`DataFrameGroupBy.agg` that raises ``AttributeError`` when there is dictionary input and duplicated columns, instead of returning a DataFrame with the aggregation of all duplicate columns. (:issue:`55041`)
572572
- Bug in :meth:`DataFrameGroupBy.apply` that was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`)
573573
- Bug in :meth:`DataFrameGroupBy.apply` with ``as_index=False`` that was returning :class:`MultiIndex` instead of returning :class:`Index`. (:issue:`58291`)
574+
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
574575
- Bug in :meth:`DataFrameGroupby.transform` and :meth:`SeriesGroupby.transform` with a reducer and ``observed=False`` that coerces dtype to float when there are unobserved categories. (:issue:`55326`)
575576
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
576577

pandas/_libs/groupby.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,12 @@ def group_cumsum(
399399
lab = labels[i]
400400

401401
if lab < 0:
402+
# GH#58811
403+
if uses_mask:
404+
result_mask[i, :] = True
405+
out[i, :] = 0
402406
continue
407+
403408
for j in range(K):
404409
val = values[i, j]
405410

pandas/tests/groupby/transform/test_transform.py

+9
Original file line numberDiff line numberDiff line change
@@ -1591,3 +1591,12 @@ def test_min_one_dim_no_type_coercion():
15911591

15921592
expected = DataFrame({"Y": [9435, -5465765, -5465765, 0, 9435]}, dtype="int32")
15931593
tm.assert_frame_equal(expected, result)
1594+
1595+
1596+
def test_nan_in_cumsum_group_label():
1597+
# GH#58811
1598+
df = DataFrame({"A": [1, None], "B": [2, 3]}, dtype="Int16")
1599+
gb = df.groupby("A")["B"]
1600+
result = gb.cumsum()
1601+
expected = Series([2, None], dtype="Int16", name="B")
1602+
tm.assert_series_equal(expected, result)

0 commit comments

Comments
 (0)