Skip to content

Behavior change of 1.4.rc0 when group by apply returns a copy of the passed data frame #45315

Closed
@aberres

Description

@aberres

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 master branch of pandas.

Reproducible Example

df = pd.DataFrame([[1,2,3,4]], columns=["A", "B", "C", "D"])

grouped = df.groupby(['A', 'B'], as_index= True)[["C", "D"]]

combined = grouped.apply(lambda x: x.copy())

combined.reset_index().columns
# Pandas 1.3: Index(['A', 'B', 'level_2', 'C', 'D'], dtype='object')
# Pandas 1.4: Index(['index', 'C', 'D'], dtype='object')

Issue Description

While testing our code base against 1.4rc0 I stumbled upon a behavior change of Pandas 1.4.

Formerly when apply was called on a grouper it seems as if a returned multi-index was honored in the resulting data frame.
With 1.4 the index is dropped.

No idea if this is an intentional change or not. Wanted to let you know to be on the safe side.
also did not check why or code does what it does (the method called in apply is pretty complex, not just a copy). Looks a bit fishy...

Expected Behavior

Unchanged behavior.

Installed Versions

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugGroupbyRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions