Skip to content

BUG: way to include all columns within a groupby apply #61406

Open
@madelavar12

Description

@madelavar12

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

# Sample DataFrame
df = pd.DataFrame({
    "group": ["A", "A", "B", "B"],
    "value": [1, 2, 3, 4],
})

# Function that operates on the whole group (e.g., adds a new column)
def process_group(group_df):
    group_df["value_doubled"] = group_df["value"] * 2
    return group_df

# Trigger the deprecation warning
result = df.groupby("group").apply(process_group)
print(result)


        group  value  value_doubled
group                              
A     0     A      1              2
      1     A      2              4
B     2     B      3              6
      3     B      4              8
C:\Users\e361154\AppData\Local\Temp\1\ipykernel_15728\2443901964.py:15: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.
  result = df.groupby("group").apply(process_group)

Issue Description

When using groupby().apply() with a function that modifies and returns the entire group DataFrame, a FutureWarning is raised in pandas >= 2.2. This warning notifies users that in pandas 3.0, the default behavior will change: the grouping columns will no longer be included in the data passed to the function unless include_groups=True is explicitly set. To maintain the current behavior and suppress the warning, users must pass include_groups=False.

This affects workflows where the function operates on the full DataFrame per group and expects the group keys to be included in the data automatically, as was the case in earlier pandas versions.

Expected Behavior

The expected behavior is still what I want from the above example. I just don't want that functionality to be lost in pandas 3.0.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.10.7
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.3
numpy : 2.2.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 8.35.0
adbc-driver-postgresql: None
...
zstandard : 0.23.0
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugClosing CandidateMay be closeable, needs more eyeballsGroupby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions