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

API: avoid passing Manager to subclass __init__ #57553

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,25 +651,36 @@ def _constructor(self) -> type[DataFrame]:
return DataFrame

def _constructor_from_mgr(self, mgr, axes) -> DataFrame:
if self._constructor is DataFrame:
# we are pandas.DataFrame (or a subclass that doesn't override _constructor)
return DataFrame._from_mgr(mgr, axes=axes)
else:
assert axes is mgr.axes
df = DataFrame._from_mgr(mgr, axes=axes)

if type(self) is DataFrame:
# This would also work `if self._constructor is DataFrame`, but
# this check is slightly faster, benefiting the most-common case.
return df

elif type(self).__name__ == "GeoDataFrame":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth having a test for this in test_downstream.py? geopandas is installed in the CI

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea (also should add a test for#57032). Will wait until Joris weighs in

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im having trouble writing a meaningful test for this cc @jorisvandenbossche

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GeoPandas is already overriding _constructor_from_mgr, so normally this shouldn't be needed.

(will try to take a closer look one of the coming days!)

# Shim until geopandas can override their _constructor_from_mgr
# bc they have different behavior for Managers than for DataFrames
return self._constructor(mgr)

_constructor_sliced: Callable[..., Series] = Series
# We assume that the subclass __init__ knows how to handle a
# pd.DataFrame object.
return self._constructor(df)

def _sliced_from_mgr(self, mgr, axes) -> Series:
return Series._from_mgr(mgr, axes)
_constructor_sliced: Callable[..., Series] = Series

def _constructor_sliced_from_mgr(self, mgr, axes) -> Series:
if self._constructor_sliced is Series:
ser = self._sliced_from_mgr(mgr, axes)
ser._name = None # caller is responsible for setting real name
ser = Series._from_mgr(mgr, axes)
ser._name = None # caller is responsible for setting real name

if type(self) is DataFrame:
# This would also work `if self._constructor_sliced is Series`, but
# this check is slightly faster, benefiting the most-common case.
return ser
assert axes is mgr.axes
return self._constructor_sliced(mgr)

# We assume that the subclass __init__ knows how to handle a
# pd.Series object.
return self._constructor_sliced(ser)

# ----------------------------------------------------------------------
# Constructors
Expand Down
1 change: 1 addition & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def _init_mgr(
mgr = mgr.astype(dtype=dtype)
return mgr

@final
@classmethod
def _from_mgr(cls, mgr: Manager, axes: list[Index]) -> Self:
"""
Expand Down
34 changes: 19 additions & 15 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,17 @@ def _constructor(self) -> type[Series]:
return Series

def _constructor_from_mgr(self, mgr, axes):
if self._constructor is Series:
# we are pandas.Series (or a subclass that doesn't override _constructor)
ser = Series._from_mgr(mgr, axes=axes)
ser._name = None # caller is responsible for setting real name
ser = Series._from_mgr(mgr, axes=axes)
ser._name = None # caller is responsible for setting real name

if type(self) is Series:
# This would also work `if self._constructor is Series`, but
# this check is slightly faster, benefiting the most-common case.
return ser
else:
assert axes is mgr.axes
return self._constructor(mgr)

# We assume that the subclass __init__ knows how to handle a
# pd.Series object.
return self._constructor(ser)

@property
def _constructor_expanddim(self) -> Callable[..., DataFrame]:
Expand All @@ -595,18 +598,19 @@ def _constructor_expanddim(self) -> Callable[..., DataFrame]:

return DataFrame

def _expanddim_from_mgr(self, mgr, axes) -> DataFrame:
def _constructor_expanddim_from_mgr(self, mgr, axes):
from pandas.core.frame import DataFrame

return DataFrame._from_mgr(mgr, axes=mgr.axes)
df = DataFrame._from_mgr(mgr, axes=mgr.axes)

def _constructor_expanddim_from_mgr(self, mgr, axes):
from pandas.core.frame import DataFrame
if type(self) is Series:
# This would also work `if self._constructor_expanddim is DataFrame`,
# but this check is slightly faster, benefiting the most-common case.
return df

if self._constructor_expanddim is DataFrame:
return self._expanddim_from_mgr(mgr, axes)
assert axes is mgr.axes
return self._constructor_expanddim(mgr)
# We assume that the subclass __init__ knows how to handle a
# pd.DataFrame object.
return self._constructor_expanddim(df)

# types
@property
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@


class TestDataFrameSubclassing:
def test_no_warning_on_mgr(self):
# GH#57032
df = tm.SubclassedDataFrame(
{"X": [1, 2, 3], "Y": [1, 2, 3]}, index=["a", "b", "c"]
)
with tm.assert_produces_warning(None):
# df.isna() goes through _constructor_from_mgr, which we want to
# *not* pass a Manager do __init__
df.isna()
df["X"].isna()

def test_frame_subclassing_and_slicing(self):
# Subclass frame and ensure it returns the right class on slicing it
# In reference to PR 9632
Expand Down