You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Whether group columns are added to the index depends on the amount fo rows returned by apply:
when it changes - group columns are added to the index. When it doesn't change - group columns aren't added.
The problem is, that amount of returned rows may or may not change after applying exactly the same code.
(There is a similar issue, that is nevertheless different. Previously, I reported this bug there. But topic starter there reported a different issue, and it is being resolved separately, as I got it: #44803)
Expected Behavior
IMO, the most intuitive behavior is always adding group columns to index.
The fact that 'transform' operations don't do it - was a major source of confustion for me and my colleagues.
Installed Versions
Replace this line with the output of pd.show_versions()
The text was updated successfully, but these errors were encountered:
Thanks for the report! This is duplicate of other issues and will be closed by #34998 (see issues linked there).
The fact that 'transform' operations don't do it - was a major source of confustion for me and my colleagues.
Transforms don't do it because they are most useful that way, as often it is used as:
df['new_column'] = df.groupby(keys).apply(op)
If the index of the RHS is not the same as that on the LHS, this would not work because of alignment. That said, I think there is an appetite to have apply not infer (at least, as much). This could mean forcing users to to use .transform when they want to apply a transformation. The drawback is this would make apply less flexible, and needs to be considered carefully.
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Whether group columns are added to the index depends on the amount fo rows returned by apply:
when it changes - group columns are added to the index. When it doesn't change - group columns aren't added.
The problem is, that amount of returned rows may or may not change after applying exactly the same code.
(There is a similar issue, that is nevertheless different. Previously, I reported this bug there. But topic starter there reported a different issue, and it is being resolved separately, as I got it: #44803)
Expected Behavior
IMO, the most intuitive behavior is always adding group columns to index.
The fact that 'transform' operations don't do it - was a major source of confustion for me and my colleagues.
Installed Versions
Replace this line with the output of pd.show_versions()
The text was updated successfully, but these errors were encountered: