RonakkGovani opened a new pull request, #60250:
URL: https://github.com/apache/spark/pull/60250
### What changes were proposed in this pull request?
Keep the `name` attribute of each group in `GroupBy.apply` and
`GroupBy.filter` in pandas API on Spark.
Before calling the function, pandas-on-Spark drops the group key columns
from each pandas group. `DataFrame.drop` returns a new DataFrame without the
`name` attribute that pandas sets to the group key. This PR adds a small helper
that carries `name` over to the frame passed to the function. It reads the
attribute from `__dict__`, so a column called `name` is not picked up instead.
### Why are the changes needed?
A function that reads the group key from `name` works in pandas but fails in
pandas-on-Spark:
```python
>>> import pandas as pd
>>> import pyspark.pandas as ps
>>> ps.DataFrame({"c0": [1]}).groupby("c0").apply(lambda g: pd.Series({"c1":
g.name}))
AttributeError: 'DataFrame' object has no attribute 'name'
```
When the group has a column called `name`, the function silently gets that
column instead of the group key.
### Does this PR introduce _any_ user-facing change?
Yes. Inside `DataFrameGroupBy.apply` and `DataFrameGroupBy.filter`, the
function can now read `name` as in pandas:
```python
>>> ps.DataFrame({"c0": [1]}).groupby("c0").apply(lambda g: pd.Series({"c1":
g.name}))
c1
c0
1 1
```
### How was this patch tested?
Added `test_apply_group_name` (with and without the shortcut path, single
and multiple keys, with and without a column named `name`) and
`test_filter_group_name` to `pyspark.pandas.tests.groupby.test_apply_func`. The
new tests fail without this change. Also ran `test_apply_func`, `test_groupby`
and `test_split_apply` locally.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Opus 5.5)
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]