Repository: dask/dask

Issue:
`cumsum` results differ from pandas for series groupbys
**What happened**:
I noticed that taking the cumulative sum of a pandas series groupby and an equivalent dask series groupby returns numerically different results.


**Minimal Complete Verifiable Example**:

```python
import pandas as pd
import numpy as np
import dask.dataframe as dd

np.random.seed(0)
size=10
npartitions=2


pdf = pd.DataFrame(
    {
        "xx": np.random.randint(0, 5, size=size),
        "x": np.random.normal(size=size),
        "y": np.random.normal(size=size),
    }
)

ddf = dd.from_pandas(pdf, npartitions=npartitions)

pdf_grouped = pdf.groupby('xx').xx
ddf_grouped = ddf.groupby('xx').xx

print(pdf_grouped.cumsum())
print(ddf_grouped.cumsum().compute())

```
```
0     4
1     0
2     3
3     6
4     9
5     1
6    12
7     2
8     8
9     0
Name: xx, dtype: int64
0    4
1    0
2    3
3    6
4    9
5    1
6    6
7    2
8    8
9    0
Name: xx, dtype: int64
```


**Anything else we need to know?**:
I get the right results when `npartitions=1.` 


**Environment**:

- Dask version: 2022.7.0+16.gedfa91e22
- Python version: 3.9.13 
- Operating System: Ubuntu 18.04.6 LTS
- Install method: conda


Discussion:
Thanks for reporting!

This only seems to affect the column we're grouping on (e.g., `ddf.groupby('xx').y.cumsum()` works fine). I'll keep looking into it.
@jrbourbeau and I looked into this. The issue seems to be in:

https://github.com/dask/dask/blob/8b95f983c232c1bd628e9cba0695d3ef229d290b/dask/dataframe/groupby.py#L1274-L1281

Consider the following example (same example as OP, only the values are hardcoded for understanding):

```python
import dask.dataframe as dd
import pandas as pd

pdf = pd.DataFrame(
    {
        "x": [4, 0, 3, 3, 3, 1, 3, 2, 4, 0],
        "a": [4, 0, 3, 3, 3, 1, 3, 2, 4, 0],
    }
)

ddf = dd.from_pandas(pdf, npartitions=2)

# CASE 1 -- cumsum on 'a' -- produces correct results
pdf_grouped = pdf.groupby("x").a.cumsum()
ddf_grouped = ddf.groupby("x").a.cumsum().compute()

# CASE 2 -- cumsum on 'x' -- produces wrong results
pdf.groupby("x").x.cumsum()
ddf.groupby("x").x.cumsum().compute()
```

The `cumpart_ext` calculated is different in both cases:

CASE 1:
```python
cumpart_ext.compute()
   a  x
0  4  4
1  0  0
2  3  3
3  6  3
4  9  3
5  1  1
6  3  3
7  2  2
8  4  4
9  0  0
```

CASE 2:
```python
cumpart_ext.compute()
   x
0  4
1  0
2  3
3  3
4  3
5  1
6  3
7  2
8  4
9  0
```

In case 2, while creating `cumpart_ext` (look at the permalink) the column 'x' is being overridden. This is because we're creating (`assign`ing) a new column with the same name as `by` (column we groupby). Does that make sense?

The solution here could be to rename the `by` column internally (like, add a suffix to it to make sure it's unique), throughout the cumulative logic.
With dask `2022.10.2` and pandas `1.5.0` the bug in the original report persists for me.
@wence- Would you mind opening a new issue, with the minimal reproducible example and linking this too. That would give more visibility. 

Resolving patch:
diff --git a/dask/dataframe/groupby.py b/dask/dataframe/groupby.py
--- a/dask/dataframe/groupby.py
+++ b/dask/dataframe/groupby.py
@@ -1466,12 +1466,20 @@ def _cum_agg(self, token, chunk, aggregate, initial):
         # rename "by" columns internally
         # to fix cumulative operations on the same "by" columns
         # ref: https://github.com/dask/dask/issues/9313
-        if columns is not None and set(columns).intersection(set(by_cols)):
+        if columns is not None:
+            # Handle series case (only a single column, but can't
+            # enlist above because that fails in pandas when
+            # constructing meta later)
+            grouping_columns = [columns] if is_series_like(meta) else columns
+            to_rename = set(grouping_columns) & set(by_cols)
             by = []
             for col in by_cols:
-                suffix = str(uuid.uuid4())
-                self.obj = self.obj.assign(**{col + suffix: self.obj[col]})
-                by.append(col + suffix)
+                if col in to_rename:
+                    suffix = str(uuid.uuid4())
+                    self.obj = self.obj.assign(**{col + suffix: self.obj[col]})
+                    by.append(col + suffix)
+                else:
+                    by.append(col)
         else:
             by = by_cols
 


Test patch:
diff --git a/dask/dataframe/tests/test_groupby.py b/dask/dataframe/tests/test_groupby.py
--- a/dask/dataframe/tests/test_groupby.py
+++ b/dask/dataframe/tests/test_groupby.py
@@ -1688,6 +1688,12 @@ def test_cumulative(func, key, sel):
             dg.cumcount(axis=0)
 
 
+def test_series_groupby_multi_character_column_name():
+    df = pd.DataFrame({"aa": [1, 2, 1, 3, 4, 1, 2]})
+    ddf = dd.from_pandas(df, npartitions=3)
+    assert_eq(df.groupby("aa").aa.cumsum(), ddf.groupby("aa").aa.cumsum())
+
+
 @pytest.mark.parametrize("func", ["cumsum", "cumprod"])
 def test_cumulative_axis1(func):
     df = pd.DataFrame(