Skip to content

Add missing __finalize__ calls in indexers/iterators #46101

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 9 commits into from
Apr 26, 2022
Merged
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Fixed metadata propagation in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` (:issue:`28283`)
Copy link
Contributor

Choose a reason for hiding this comment

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

pls move to 1.5

-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
columns = self.columns
klass = self._constructor_sliced
for k, v in zip(self.index, self.values):
s = klass(v, index=columns, name=k)
s = klass(v, index=columns, name=k).__finalize__(self)
yield k, s

def itertuples(
Expand Down Expand Up @@ -3453,7 +3453,7 @@ def _ixs(self, i: int, axis: int = 0):
index=self.columns,
name=self.index[i],
dtype=new_values.dtype,
)
).__finalize__(self)
result._set_is_copy(self, copy=copy)
return result

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3816,7 +3816,7 @@ class animal locomotion
index=self.columns,
name=self.index[loc],
dtype=new_values.dtype,
)
).__finalize__(self)
elif is_scalar(loc):
result = self.iloc[:, slice(loc, loc + 1)]
elif axis == 1:
Expand Down
9 changes: 2 additions & 7 deletions pandas/tests/generic/test_duplicate_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def test_binops(self, func, other, frame):
assert df.flags.allows_duplicate_labels is False
assert func(df).flags.allows_duplicate_labels is False

@not_implemented
def test_preserve_getitem(self):
df = pd.DataFrame({"A": [1, 2]}).set_flags(allows_duplicate_labels=False)
assert df[["A"]].flags.allows_duplicate_labels is False
Expand Down Expand Up @@ -306,15 +305,11 @@ def test_series_raises(self):
(operator.itemgetter(["A", "A"]), None),
# loc
(operator.itemgetter(["a", "a"]), "loc"),
pytest.param(
operator.itemgetter(("a", ["A", "A"])), "loc", marks=not_implemented
),
pytest.param(operator.itemgetter(("a", ["A", "A"])), "loc"),
(operator.itemgetter((["a", "a"], "A")), "loc"),
# iloc
(operator.itemgetter([0, 0]), "iloc"),
pytest.param(
operator.itemgetter((0, [0, 0])), "iloc", marks=not_implemented
),
pytest.param(operator.itemgetter((0, [0, 0])), "iloc"),
pytest.param(operator.itemgetter(([0, 0], 0)), "iloc"),
],
)
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@
frame_data,
operator.methodcaller("quantile", numeric_only=True),
),
marks=not_implemented_mark,
),
pytest.param(
(
Expand All @@ -265,10 +264,9 @@
pytest.param(
(
pd.DataFrame,
frame_data,
operator.methodcaller("quantile", numeric_only=True),
({"A": [pd.Timedelta(days=1), pd.Timedelta(days=2)]},),
operator.methodcaller("quantile", numeric_only=False),
),
marks=not_implemented_mark,
),
(
pd.DataFrame,
Expand Down