Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFrame align() on a subclassed DataFrame does not invoke _constructor() for the subclassed DataFrame #12983

Closed
amanhanda opened this issue Apr 25, 2016 · 2 comments
Labels
Compat pandas objects compatability with Numpy or Python functions
Milestone

Comments

@amanhanda
Copy link

# my_dataframe.py
import pandas as pd


class MySeries(pd.Series):
    @property
    def _constructor(self):
        return MySeries

    @property
    def _constructor_expanddim(self):
        return MyDataFrame


class MyDataFrame(pd.DataFrame):
    def __init__(self, data=None, index=None, columns=None, dtype=None, copy=False, **kwargs):
        super(MyDataFrame, self).__init__(data=data, index=index, columns=columns, dtype=dtype, copy=copy, **kwargs)

    @property
    def _constructor(self):
        return MyDataFrame

    @property
    def _constructor_sliced(self):
        return MySeries

    @property
    def _constructor_expanddim(self):
        return MyPanel


class MyPanel(pd.Panel):
    def __init__(self, data=None, items=None, major_axis=None, minor_axis=None, copy=False, dtype=None, **kwargs):
        super(MyPanel, self).__init__(data=data, items=items, major_axis=major_axis, minor_axis=minor_axis, copy=copy,
                                      dtype=dtype, **kwargs)

    @property
    def _constructor(self):
        return MyPanel

    @property
    def _constructor_sliced(self):
        return MyDataFrame

def f():
    df1 = MyDataFrame(pd.DataFrame([1, 2, 3, 4, 5], columns=['A']))
    df2 = MyDataFrame(pd.DataFrame([1, 2, 3, 4, 5], columns=['A']))
    print type(df1.align(df2, axis=0)[0])  # Note, if aligning with frame, it works fine, returns MyDataFrame
    print type(df1.align(df2.A, axis=0)[0])  # But if aligning with series, it does not, returns DataFrame

# import my_dataframe
 my_dataframe.f()
<class 'com.utils.my_dataframe.MyDataFrame'> # Correct 
<class 'pandas.core.frame.DataFrame'> # Does not invoke _constructor()

When aligning with a series, the code seems to explicitly create the DataFrame using DataFrame() constructor.

@jreback
Copy link
Contributor

jreback commented Apr 25, 2016

is this not covered in: #12308 (or several recently merged issues)?

@jreback
Copy link
Contributor

jreback commented Apr 25, 2016

@sinhrks

@jreback jreback added the Compat pandas objects compatability with Numpy or Python functions label Apr 25, 2016
@jreback jreback added this to the Next Major Release milestone Apr 25, 2016
@jreback jreback modified the milestones: 0.18.1, Next Major Release Apr 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants