Skip to content

Commit 17d2428

Browse files
authored
Add missing __finalize__ calls in indexers/iterators (#46101)
1 parent 8980af7 commit 17d2428

File tree

5 files changed

+8
-14
lines changed

5 files changed

+8
-14
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ Indexing
542542
- Bug in :meth:`CategoricalIndex.get_indexer` when index contains ``NaN`` values, resulting in elements that are in target but not present in the index to be mapped to the index of the NaN element, instead of -1 (:issue:`45361`)
543543
- Bug in setting large integer values into :class:`Series` with ``float32`` or ``float16`` dtype incorrectly altering these values instead of coercing to ``float64`` dtype (:issue:`45844`)
544544
- Bug in :meth:`Series.asof` and :meth:`DataFrame.asof` incorrectly casting bool-dtype results to ``float64`` dtype (:issue:`16063`)
545+
- Bug in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` not always propagating metadata (:issue:`28283`)
545546
-
546547

547548
Missing

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
13391339
columns = self.columns
13401340
klass = self._constructor_sliced
13411341
for k, v in zip(self.index, self.values):
1342-
s = klass(v, index=columns, name=k)
1342+
s = klass(v, index=columns, name=k).__finalize__(self)
13431343
yield k, s
13441344

13451345
def itertuples(
@@ -3455,7 +3455,7 @@ def _ixs(self, i: int, axis: int = 0):
34553455
index=self.columns,
34563456
name=self.index[i],
34573457
dtype=new_values.dtype,
3458-
)
3458+
).__finalize__(self)
34593459
result._set_is_copy(self, copy=copy)
34603460
return result
34613461

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3816,7 +3816,7 @@ class animal locomotion
38163816
index=self.columns,
38173817
name=self.index[loc],
38183818
dtype=new_values.dtype,
3819-
)
3819+
).__finalize__(self)
38203820
elif is_scalar(loc):
38213821
result = self.iloc[:, slice(loc, loc + 1)]
38223822
elif axis == 1:

pandas/tests/generic/test_duplicate_labels.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def test_binops(self, func, other, frame):
8484
assert df.flags.allows_duplicate_labels is False
8585
assert func(df).flags.allows_duplicate_labels is False
8686

87-
@not_implemented
8887
def test_preserve_getitem(self):
8988
df = pd.DataFrame({"A": [1, 2]}).set_flags(allows_duplicate_labels=False)
9089
assert df[["A"]].flags.allows_duplicate_labels is False
@@ -306,15 +305,11 @@ def test_series_raises(self):
306305
(operator.itemgetter(["A", "A"]), None),
307306
# loc
308307
(operator.itemgetter(["a", "a"]), "loc"),
309-
pytest.param(
310-
operator.itemgetter(("a", ["A", "A"])), "loc", marks=not_implemented
311-
),
308+
pytest.param(operator.itemgetter(("a", ["A", "A"])), "loc"),
312309
(operator.itemgetter((["a", "a"], "A")), "loc"),
313310
# iloc
314311
(operator.itemgetter([0, 0]), "iloc"),
315-
pytest.param(
316-
operator.itemgetter((0, [0, 0])), "iloc", marks=not_implemented
317-
),
312+
pytest.param(operator.itemgetter((0, [0, 0])), "iloc"),
318313
pytest.param(operator.itemgetter(([0, 0], 0)), "iloc"),
319314
],
320315
)

pandas/tests/generic/test_finalize.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@
247247
frame_data,
248248
operator.methodcaller("quantile", numeric_only=True),
249249
),
250-
marks=not_implemented_mark,
251250
),
252251
pytest.param(
253252
(
@@ -259,10 +258,9 @@
259258
pytest.param(
260259
(
261260
pd.DataFrame,
262-
frame_data,
263-
operator.methodcaller("quantile", numeric_only=True),
261+
({"A": [pd.Timedelta(days=1), pd.Timedelta(days=2)]},),
262+
operator.methodcaller("quantile", numeric_only=False),
264263
),
265-
marks=not_implemented_mark,
266264
),
267265
(
268266
pd.DataFrame,

0 commit comments

Comments
 (0)