diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index e4879a6c41515..23d88b2626ad7 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -542,6 +542,7 @@ Indexing - 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`) - 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`) - Bug in :meth:`Series.asof` and :meth:`DataFrame.asof` incorrectly casting bool-dtype results to ``float64`` dtype (:issue:`16063`) +- Bug in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` not always propagating metadata (:issue:`28283`) - Missing diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 74d061cbb9b7f..256f5b1358eda 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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( @@ -3455,7 +3455,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 diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 473435bae48eb..c615216240d60 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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: diff --git a/pandas/tests/generic/test_duplicate_labels.py b/pandas/tests/generic/test_duplicate_labels.py index 189c5382ef114..c83c8e1d568e6 100644 --- a/pandas/tests/generic/test_duplicate_labels.py +++ b/pandas/tests/generic/test_duplicate_labels.py @@ -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 @@ -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"), ], ) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 999fee5c8281c..540a30e691d3c 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -247,7 +247,6 @@ frame_data, operator.methodcaller("quantile", numeric_only=True), ), - marks=not_implemented_mark, ), pytest.param( ( @@ -259,10 +258,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,