Skip to content

BUG: remove usage of _constructor._get_axis_number (fix when _constructor properties return callables) #46018

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,23 +803,23 @@ class SubclassedSeries(Series):

@property
def _constructor(self):
return SubclassedSeries
return lambda *args, **kwargs: SubclassedSeries(*args, **kwargs)

@property
def _constructor_expanddim(self):
return SubclassedDataFrame
return lambda *args, **kwargs: SubclassedDataFrame(*args, **kwargs)


class SubclassedDataFrame(DataFrame):
_metadata = ["testattr"]

@property
def _constructor(self):
return SubclassedDataFrame
return lambda *args, **kwargs: SubclassedDataFrame(*args, **kwargs)

@property
def _constructor_sliced(self):
return SubclassedSeries
return lambda *args, **kwargs: SubclassedSeries(*args, **kwargs)


class SubclassedCategorical(Categorical):
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6608,8 +6608,10 @@ def replace(

if isinstance(to_replace, (tuple, list)):
if isinstance(self, ABCDataFrame):
from pandas import Series

result = self.apply(
self._constructor_sliced._replace_single,
Series._replace_single,
args=(to_replace, method, inplace, limit),
)
if inplace:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ def _cumcount_array(self, ascending: bool = True) -> np.ndarray:

@final
@property
def _obj_1d_constructor(self) -> type[Series]:
def _obj_1d_constructor(self) -> Callable:
# GH28330 preserve subclassed Series/DataFrames
if isinstance(self.obj, DataFrame):
return self.obj._constructor_sliced
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections import abc
from typing import (
TYPE_CHECKING,
Callable,
Hashable,
Iterable,
Literal,
Expand Down Expand Up @@ -467,7 +468,9 @@ def __init__(

# Standardize axis parameter to int
if isinstance(sample, ABCSeries):
axis = sample._constructor_expanddim._get_axis_number(axis)
from pandas import DataFrame

axis = DataFrame._get_axis_number(axis)
else:
axis = sample._get_axis_number(axis)

Expand Down Expand Up @@ -539,7 +542,7 @@ def __init__(
self.new_axes = self._get_new_axes()

def get_result(self):
cons: type[DataFrame | Series]
cons: Callable
Copy link
Member

Choose a reason for hiding this comment

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

could annotate as Callable returning DataFrame|Series

Copy link
Member Author

Choose a reason for hiding this comment

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

The Callable typing is actually giving errors .. (with or without return type)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, but I probably have to update the actual properties' annotation to callable as well

sample: DataFrame | Series

# series only
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,11 @@ def test_equals_subclass(self):
df2 = tm.SubclassedDataFrame({"a": [1, 2, 3]})
assert df1.equals(df2)
assert df2.equals(df1)

def test_replace_list_method(self):
# https://github.com/pandas-dev/pandas/pull/46018
df = tm.SubclassedDataFrame({"A": [0, 1, 2]})
result = df.replace([1, 2], method="ffill")
expected = tm.SubclassedDataFrame({"A": [0, 0, 0]})
assert isinstance(result, tm.SubclassedDataFrame)
tm.assert_frame_equal(result, expected)