Skip to content

BUG: DataFrameGroupBy.apply does not take into account the value of as_index field #53649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
anmyachev opened this issue Jun 13, 2023 · 2 comments
Closed
2 of 3 tasks
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby

Comments

@anmyachev
Copy link
Contributor

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

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "col1": [0, 1, 2, 3],
        "col2": [4, 5, np.NaN, 7],
        "col3": [np.NaN, np.NaN, 12, 10],
        "col4": [17, 13, 16, 15],
        "col5": [-4, -5, -6, -7],
    }
)

print(df.groupby(by=[1,2,1,2], as_index=False).apply(lambda df: df.sum()))
print(df.groupby(by=[1,2,1,2], as_index=True).apply(lambda df: df.sum()))

Issue Description

as_index parameter does not affect the result of apply.

Expected Behavior

>>> pandas_df.groupby(by=[1,2,1,2], as_index=True).apply(lambda df: df.sum())
   col1  col2  col3  col4  col5
1   2.0   4.0  12.0  33.0 -10.0
2   4.0  12.0  10.0  28.0 -12.0

>>> pandas_df.groupby(by=[1,2,1,2], as_index=False).apply(lambda df: df.sum())   # <- expected output
   index  col1  col2  col3  col4  col5
0      1   2.0   4.0  12.0  33.0 -10.0
1      2   4.0  12.0  10.0  28.0 -12.0

Installed Versions

pandas 2.0.2
-- Replace this line with the output of pd.show_versions()

@anmyachev anmyachev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 13, 2023
@rhshadrach
Copy link
Member

rhshadrach commented Jun 14, 2023

Thanks for the report! Due to #52333 the following warning is now emitted on the as_index=False line:

FutureWarning: A grouping was used that is not in the columns of the DataFrame and so was excluded from the result. This grouping will be included in a future version of pandas. Add the grouping as a column of the DataFrame to silence this warning.

This deprecation will appear in 2.1. Once the deprecation is enforced in pandas 3.0, this will be fixed.

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 14, 2023
@anmyachev
Copy link
Contributor Author

Thanks for the report! Due to #52333 the following warning is now emitted on the as_index=False line:

FutureWarning: A grouping was used that is not in the columns of the DataFrame and so was excluded from the result. This grouping will be included in a future version of pandas. Add the grouping as a column of the DataFrame to silence this warning.

This deprecation will appear in 2.1. Once the deprecation is enforced in pandas 3.0, this will be fixed.

@rhshadrach thanks for the answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby
Projects
None yet
Development

No branches or pull requests

2 participants