Skip to content

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

Open
@xma08

Description

@xma08

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds InfoClarification about behavior needed to assess issueWarningsWarnings that appear or should be added to pandas

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions