Skip to content

BUG: groupby.agg raises KeyError: "['col1', 'col5'] not found in axis" #52760

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 numpy as np
import pandas as pd

pandas_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],
    }
)

pandas_df = pandas_df.astype({"col1": "category"})
pandas_df["col1"] = pandas_df["col1"].cat.as_ordered()
pandas_groupby = pandas_df.groupby(by=["col1", "col5"], as_index=False)
print(pandas_groupby["col3"].agg(["mean"]))  # <-- *** KeyError: "['col1', 'col5'] not found in axis" (in pandas==2.0.0)

Issue Description

The example works fine in pandas==1.5.3 but doesn't work in pandas==2.0.0.

Expected Behavior

I think the test should work for pandas 2.0.0.

Installed Versions

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

Metadata

Metadata

Assignees

Labels

ApplyApply, Aggregate, Transform, MapBugCategoricalCategorical Data TypeGroupbyRegressionFunctionality 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