Skip to content

BUG: df.agg raises Exception on valid function in df.apply #45800

Open
@attack68

Description

@attack68

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

.

Issue Description

If the following agg is performed it currently works but gives a warning:

df = pd.DataFrame({
    "A": Series((1000, 2000), dtype=int),
    "B": Series((1000, 2000), dtype=np.int64),
    "C": Series(["a", "b"]),
})

df.agg(["mean", "sum"])
           A       B    C
mean  1500.0  1500.0  NaN
sum   3000.0  3000.0   ab

FutureWarning: ['C'] did not aggregate successfully. If any error is raised this will raise in a future version of pandas. Drop these columns/ops to avoid this warning. print(df.agg(["mean", "sum"]))

However, I do not want to:

  • drop column 'C', because there is at least one op, sum, which produces a valid results for that column.
  • drop op 'mean' because there is at least one column, 'A', 'B', which produce valid results for that op.

I tried to design a function which would error trap this:

def mean2(s:Series):
    try:
        ret = s.mean()
    except Exception:
        ret = pd.NA
    return ret

df.agg([mean2, "sum"])
<ValueError: cannot combine transform and aggregation operations>

Oddly, this works with apply which is what the agg docs give guidance on:

df.apply(mean2, axis=0)
A    1500.0
B    1500.0
C      <NA>
dtype: object

So what's the solution here?

Expected Behavior

.

Installed Versions

.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions