Skip to content

BUG: the behavior of DataFrameGroupBy.apply(..., include_groups=True) breaks post-mortem debugging #61627

Open
@pulkin

Description

@pulkin

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

def f(df):
    df["group"]
    raise TypeError("a very subtle bug")

pd.DataFrame({"group": ["a", "a", "b", "b"], "data": [0, 1, 2, 3]}).groupby("group").apply(f)

Issue Description

The argument in the title and the corresponding behavior is described like this:

When True, will attempt to apply func to the groupings in the case that they are columns of the DataFrame.
If this raises a TypeError, the result will be computed with the groupings excluded. When False,
the groupings will be excluded when applying func.

https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.DataFrameGroupBy.apply.html

I think the described behavior is problematic and it renders close to impossible to use post-mortem debugging for the TypeError("a very subtle bug"). pandas should not swallow TypeError hoping that developers will figure it out in a pile of logs.

Expected Behavior

There should be no "attempts" from the docs and pandas should not catch and swallow any exceptions from the payload.

Installed Versions

INSTALLED VERSIONS

commit : 2cc3762
python : 3.13.3
python-bits : 64
OS : Linux
OS-release : 6.14.9-300.fc42.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Thu May 29 14:27:53 UTC 2025
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.0
numpy : 2.3.0
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 20.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions