Skip to content

BUG: fix __finalize__ xfails #45721

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 1 commit into from
Jan 30, 2022
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
21 changes: 12 additions & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9478,7 +9478,7 @@ def _series_round(ser: Series, decimals: int):
if len(new_cols) > 0:
return self._constructor(
concat(new_cols, axis=1), index=self.index, columns=self.columns
)
).__finalize__(self, method="round")
else:
return self

Expand Down Expand Up @@ -9866,7 +9866,8 @@ def count(
FutureWarning,
stacklevel=find_stack_level(),
)
return self._count_level(level, axis=axis, numeric_only=numeric_only)
res = self._count_level(level, axis=axis, numeric_only=numeric_only)
return res.__finalize__(self, method="count")

if numeric_only:
frame = self._get_numeric_data()
Expand All @@ -9889,7 +9890,7 @@ def count(
counts, index=frame._get_agg_axis(axis)
)

return result.astype("int64")
return result.astype("int64").__finalize__(self, method="count")

def _count_level(self, level: Level, axis: int = 0, numeric_only: bool = False):
if numeric_only:
Expand Down Expand Up @@ -10519,13 +10520,14 @@ def quantile(
dtype = cdtype

if is_list_like(q):
return self._constructor([], index=q, columns=cols, dtype=dtype)
res = self._constructor([], index=q, columns=cols, dtype=dtype)
return res.__finalize__(self, method="quantile")
return self._constructor_sliced([], index=cols, name=q, dtype=dtype)

res = data._mgr.quantile(qs=q, axis=1, interpolation=interpolation)

result = self._constructor(res)
return result
return result.__finalize__(self, method="quantile")

@doc(NDFrame.asfreq, **_shared_doc_kwargs)
def asfreq(
Expand Down Expand Up @@ -10742,7 +10744,7 @@ def isin(self, values) -> DataFrame:
from pandas.core.reshape.concat import concat

values = collections.defaultdict(list, values)
return concat(
result = concat(
(
self.iloc[:, [i]].isin(values[col])
for i, col in enumerate(self.columns)
Expand All @@ -10752,23 +10754,24 @@ def isin(self, values) -> DataFrame:
elif isinstance(values, Series):
if not values.index.is_unique:
raise ValueError("cannot compute isin with a duplicate axis.")
return self.eq(values.reindex_like(self), axis="index")
result = self.eq(values.reindex_like(self), axis="index")
elif isinstance(values, DataFrame):
if not (values.columns.is_unique and values.index.is_unique):
raise ValueError("cannot compute isin with a duplicate axis.")
return self.eq(values.reindex_like(self))
result = self.eq(values.reindex_like(self))
else:
if not is_list_like(values):
raise TypeError(
"only list-like or dict-like objects are allowed "
"to be passed to DataFrame.isin(), "
f"you passed a '{type(values).__name__}'"
)
return self._constructor(
result = self._constructor(
algorithms.isin(self.values.ravel(), values).reshape(self.shape),
self.index,
self.columns,
)
return result.__finalize__(self, method="isin")

# ----------------------------------------------------------------------
# Add index and columns
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ def __abs__(self: NDFrameT) -> NDFrameT:

@final
def __round__(self: NDFrameT, decimals: int = 0) -> NDFrameT:
return self.round(decimals)
return self.round(decimals).__finalize__(self, method="__round__")

# -------------------------------------------------------------------------
# Label or Level Combination Helpers
Expand Down Expand Up @@ -10383,7 +10383,7 @@ def pct_change(
# We want to restore the original index
rs = rs.loc[~rs.index.duplicated()]
rs = rs.reindex_like(data)
return rs
return rs.__finalize__(self, method="pct_change")

@final
def _agg_by_level(
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,8 @@ def to_frame(self, name: Hashable = lib.no_default) -> DataFrame:
columns = Index([name])

mgr = self._mgr.to_2d_mgr(columns)
return self._constructor_expanddim(mgr)
df = self._constructor_expanddim(mgr)
return df.__finalize__(self, method="to_frame")

def _set_name(self, name, inplace=False) -> Series:
"""
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/generic/test_duplicate_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ def test_preserved_frame(self):
assert df.loc[["a"]].flags.allows_duplicate_labels is False
assert df.loc[:, ["A", "B"]].flags.allows_duplicate_labels is False

@not_implemented
def test_to_frame(self):
s = pd.Series(dtype=float).set_flags(allows_duplicate_labels=False)
assert s.to_frame().flags.allows_duplicate_labels is False
ser = pd.Series(dtype=float).set_flags(allows_duplicate_labels=False)
assert ser.to_frame().flags.allows_duplicate_labels is False

@pytest.mark.parametrize("func", ["add", "sub"])
@pytest.mark.parametrize(
Expand Down
28 changes: 5 additions & 23 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,9 @@
(pd.Series, ([0],), operator.methodcaller("take", [])),
(pd.Series, ([0],), operator.methodcaller("__getitem__", [True])),
(pd.Series, ([0],), operator.methodcaller("repeat", 2)),
pytest.param(
(pd.Series, ([0],), operator.methodcaller("reset_index")),
marks=pytest.mark.xfail,
),
(pd.Series, ([0],), operator.methodcaller("reset_index")),
(pd.Series, ([0],), operator.methodcaller("reset_index", drop=True)),
pytest.param(
(pd.Series, ([0],), operator.methodcaller("to_frame")), marks=pytest.mark.xfail
),
(pd.Series, ([0],), operator.methodcaller("to_frame")),
(pd.Series, ([0, 0],), operator.methodcaller("drop_duplicates")),
(pd.Series, ([0, 0],), operator.methodcaller("duplicated")),
(pd.Series, ([0, 0],), operator.methodcaller("round")),
Expand Down Expand Up @@ -205,7 +200,6 @@
),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("round", 2)),
marks=not_implemented_mark,
),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("corr")),
Expand All @@ -228,12 +222,10 @@
),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("count")),
marks=not_implemented_mark,
),
pytest.param(
(pd.DataFrame, frame_mi_data, operator.methodcaller("count", level="A")),
marks=[
not_implemented_mark,
pytest.mark.filterwarnings("ignore:Using the level keyword:FutureWarning"),
],
),
Expand Down Expand Up @@ -261,7 +253,6 @@
),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("quantile", q=[0.25, 0.75])),
marks=not_implemented_mark,
),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("quantile")),
Expand All @@ -279,19 +270,16 @@
),
pytest.param(
(pd.DataFrame, frame_mi_data, operator.methodcaller("isin", [1])),
marks=not_implemented_mark,
),
pytest.param(
(pd.DataFrame, frame_mi_data, operator.methodcaller("isin", pd.Series([1]))),
marks=not_implemented_mark,
),
pytest.param(
(
pd.DataFrame,
frame_mi_data,
operator.methodcaller("isin", pd.DataFrame({"A": [1]})),
),
marks=not_implemented_mark,
),
(pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)),
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
Expand All @@ -300,10 +288,7 @@
(pd.DataFrame, frame_data, operator.methodcaller("squeeze")),
marks=not_implemented_mark,
),
pytest.param(
(pd.Series, ([1, 2],), operator.methodcaller("squeeze"))
# marks=not_implemented_mark,
),
(pd.Series, ([1, 2],), operator.methodcaller("squeeze")),
(pd.Series, ([1, 2],), operator.methodcaller("rename_axis", index="a")),
(pd.DataFrame, frame_data, operator.methodcaller("rename_axis", columns="a")),
# Unary ops
Expand All @@ -315,7 +300,7 @@
(pd.Series, [1], operator.inv),
(pd.DataFrame, frame_data, abs),
(pd.Series, [1], abs),
pytest.param((pd.DataFrame, frame_data, round), marks=not_implemented_mark),
pytest.param((pd.DataFrame, frame_data, round)),
(pd.Series, [1], round),
(pd.DataFrame, frame_data, operator.methodcaller("take", [0, 0])),
(pd.DataFrame, frame_mi_data, operator.methodcaller("xs", "a")),
Expand Down Expand Up @@ -461,10 +446,7 @@
marks=not_implemented_mark,
),
(pd.Series, ([1, 2],), operator.methodcaller("pct_change")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("pct_change")),
marks=not_implemented_mark,
),
(pd.DataFrame, frame_data, operator.methodcaller("pct_change")),
(pd.Series, ([1],), operator.methodcaller("transform", lambda x: x - x.min())),
pytest.param(
(
Expand Down