Skip to content

Preserve subclass family on reshape operations #15563

Closed
@delgadom

Description

@delgadom

Subclassed Series and DataFrames with custom _constructor, _constructor_sliced, and _constructor_expanddim methods return pandas Series and DataFrame objects on reshape operations:

>>> import numpy as np, pandas as pd
>>> import pandas.util.testing as tm
>>> df = tm.SubclassedDataFrame(np.random.random((4,5)))
>>> df
          0         1         2         3         4
0  0.151944  0.948561  0.639122  0.718071  0.296193
1  0.004606  0.978378  0.827614  0.479320  0.495945
2  0.158874  0.535476  0.173194  0.065292  0.365851
3  0.772392  0.105038  0.671064  0.164165  0.795803
>>> type(df)
<class 'pandas.util.testing.SubclassedDataFrame'>

Slicing operations use _constructor_sliced:

>>> type(df[0])
<class 'pandas.util.testing.SubclassedSeries'>

Reshape operations return regular pandas objects:

>>> type(df.stack())
<class 'pandas.core.series.Series'>

It would be great if this returned a SublcassedSeries. Same goes for unstack and pivot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignReshapingConcat, Merge/Join, Stack/Unstack, ExplodeSubclassingSubclassing pandas objects

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions