Skip to content

ENH: Return EAs from DataFrame reductions #53547

@andrewgsavage

Description

@andrewgsavage

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I'd like pandas to return an EA stored in a series when a reduction is applied to a DataFrame containing only that EA.

import pandas as pd
import pint_pandas

pd.DataFrame([[0, 1, 2]]).astype("pint[m]").sum()
0    0 meter
1    1 meter
2    2 meter
dtype: object

I would like this to return

0    0
1    1
2    2
dtype: pint[meter]

Feature Description

Store the reduction results in the same EA type as the EA values the reduction was called on.

Change https://github.com/pandas-dev/pandas/blob/main/pandas/core/internals/blocks.py#L345
to

res_values = type(self.values)([result])

Alternative Solutions

N/A

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions