Assignees
@luke396
Labels
ApplyApply, Aggregate, Transform, MapBug

Comments

@jbrockmendel
df = pd.DataFrame(
    {"A": [None, 2, 3], "B": [1.0, np.nan, 3.0], "C": ["foo", None, "bar"]}
)
df.columns = ["A", "A", "C"]

result = df.agg({"A": "count"})  # same with 'apply' instead of 'agg'
expected = df["A"].count()
tm.assert_series_equal(result, expected)

This goes through Apply.agg_dict_like, which does

results = {
    key: obj._gotitem(key, ndim=1).agg(how) for key, how in arg.items()
}

Which operates column-by-column on the relevant columns if columns are unique. But with a repeated column obj._gotitem returns a DataFrame, so the .agg returns a DataFrame.

No existing test cases get here with non-unique columns, or with Resample/GroupBy objects whose underlying object has non-unique columns.

cc @rhshadrach

@jbrockmendeljbrockmendel added Bug Needs TriageIssue that has not been reviewed by a pandas team memberlabels Feb 1, 2023
@rhshadrachrhshadrach added ApplyApply, Aggregate, Transform, Mapand removed Needs TriageIssue that has not been reviewed by a pandas team memberlabels Feb 1, 2023
@rhshadrach

I think we can resolve this by taking the indices within columns for each key and iterating through these rather than relying on _gotitem.

@luke396

take

Sign up for free to join this conversation on . Already have an account? Sign in to comment
ApplyApply, Aggregate, Transform, MapBug
None yet

Successfully merging a pull request may close this issue.