ueshin commented on a change in pull request #34160:
URL: https://github.com/apache/spark/pull/34160#discussion_r720522774
##########
File path: python/pyspark/pandas/groupby.py
##########
@@ -1295,6 +1303,8 @@ def wrapped_func(
pdf_or_ser =
pdf.groupby(groupkey_names)[name].apply(wrapped_func, *args, **kwargs)
else:
pdf_or_ser = pdf.groupby(groupkey_names).apply(wrapped_func,
*args, **kwargs)
+ if should_return_series and isinstance(pdf_or_ser,
pd.DataFrame):
Review comment:
No, I don't think it's related.
Pandas' `DataFrameGroupBy.apply` sometimes behaves weirdly when the udf
returns `Series` and whether there is only one group or more. E.g.,:
```py
>>> pdf = pd.DataFrame(
... {"a": [1, 2, 3, 4, 5, 6], "b": [1, 1, 2, 3, 5, 8], "c": [1, 4, 9,
16, 25, 36]},
... columns=["a", "b", "c"],
... )
>>> pdf.groupby('b').apply(lambda x: x['a'])
b
1 0 1
1 2
2 2 3
3 3 4
5 4 5
8 5 6
Name: a, dtype: int64
>>> pdf[pdf['b'] == 1].groupby('b').apply(lambda x: x['a'])
a 0 1
b
1 1 2
```
As you can see, if there is only one group, it returns a "wide" `DataFrame`
instead of `Series`.
In our non-shortcut path, there is always only one group because it will be
run in `groupby-applyInPandas`, so we will get `DataFrame`, then we should
convert it to `Series` ourselves.
--
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]