Skip to content

BUG: PerformanceWarning when agg with pd.NamedAgg and as_index=False #61628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
xma08 opened this issue Jun 10, 2025 · 0 comments
Open
3 tasks done

BUG: PerformanceWarning when agg with pd.NamedAgg and as_index=False #61628

xma08 opened this issue Jun 10, 2025 · 0 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@xma08
Copy link

xma08 commented Jun 10, 2025

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

n_rows = 1_000
group_size = 10
n_random_cols = 200
data = {"id": np.repeat(np.arange(n_rows // group_size), group_size)}
for i in range(n_random_cols):
    data[f"col_{i}"] = np.random.randn(n_rows)
df = pd.DataFrame(data)


# PerformanceWarning when as_index is False
named_agg_without_index_warning_df = (
    df
    .groupby('id', as_index=False)
    .agg(**{
        column: pd.NamedAgg(column=column, aggfunc="mean")
        for column in df.columns if column != "id"
    })
)

# no warnings when as_index is True
named_agg_with_index_ok_df = (
    df
    .groupby('id', as_index=True)
    .agg(**{
        column: pd.NamedAgg(column=column, aggfunc="mean")
        for column in df.columns if column != "id"
    })
)

# no warnings when using dict agg no matter what as_index is
dict_agg_ok_df = (
    df
    .groupby('id', as_index=False)
    .agg({
        column: "mean"
        for column in df.columns if column != "id"
    })
)

Issue Description

there is an inconsistent behavior (PerformanceWarning) of agg when as_index is True/False. Please refer to the example above.

Expected Behavior

No PerformanceWarning is raised when as_index=False

Installed Versions

v2.3.0

@xma08 xma08 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant