Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] 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()

Comment From: rhshadrach

Thanks for the report! This is due to #50744 - we evaluate whether a grouping is "in-axis" or not when the groupby instance is first created, and don't reevaluate this upon column selection. We end up thinking a grouping is in-axis and try to drop it because of as_index=False.