Skip to content

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

Closed
@anmyachev

Description

@anmyachev

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugGroupby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions