Skip to content

Commit 7e96ad6

Browse files
Fixed metadata propagation in DataFrame.__getitem__ (#37037)
1 parent 1ee2bda commit 7e96ad6

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ Other
699699
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` incorrectly casting from ``PeriodDtype`` to object dtype (:issue:`34871`)
700700
- Fixed bug in metadata propagation incorrectly copying DataFrame columns as metadata when the column name overlaps with the metadata name (:issue:`37037`)
701701
- Fixed metadata propagation in the :class:`Series.dt`, :class:`Series.str` accessors, :class:`DataFrame.duplicated`, :class:`DataFrame.stack`, :class:`DataFrame.unstack`, :class:`DataFrame.pivot`, :class:`DataFrame.append`, :class:`DataFrame.diff`, :class:`DataFrame.applymap` and :class:`DataFrame.update` methods (:issue:`28283`) (:issue:`37381`)
702+
- Fixed metadata propagation when selecting columns from a DataFrame with ``DataFrame.__getitem__`` (:issue:`28283`)
702703
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)
703704
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError``, from a bare ``Exception`` previously (:issue:`35744`)
704705
- Bug in ``accessor.DirNamesMixin``, where ``dir(obj)`` wouldn't show attributes defined on the instance (:issue:`37173`).

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3785,7 +3785,7 @@ def _get_item_cache(self, item):
37853785

37863786
loc = self.columns.get_loc(item)
37873787
values = self._mgr.iget(loc)
3788-
res = self._box_col_values(values, loc)
3788+
res = self._box_col_values(values, loc).__finalize__(self)
37893789

37903790
cache[item] = res
37913791
res._set_as_cached(item, self)

pandas/tests/generic/test_duplicate_labels.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,7 @@ def test_series_raises(self, func):
312312
pytest.param(
313313
operator.itemgetter(("a", ["A", "A"])), "loc", marks=not_implemented
314314
),
315-
pytest.param(
316-
operator.itemgetter((["a", "a"], "A")), "loc", marks=not_implemented
317-
),
315+
(operator.itemgetter((["a", "a"], "A")), "loc"),
318316
# iloc
319317
(operator.itemgetter([0, 0]), "iloc"),
320318
pytest.param(

pandas/tests/generic/test_finalize.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,12 @@
8585
marks=pytest.mark.xfail(reason="Implement binary finalize"),
8686
),
8787
(pd.DataFrame, frame_data, operator.methodcaller("transpose")),
88-
pytest.param(
89-
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", "A")),
90-
marks=not_implemented_mark,
91-
),
88+
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", "A")),
9289
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", ["A"])),
9390
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", np.array([True]))),
9491
(pd.DataFrame, ({("A", "a"): [1]},), operator.methodcaller("__getitem__", ["A"])),
9592
(pd.DataFrame, frame_data, operator.methodcaller("query", "A == 1")),
96-
pytest.param(
97-
(pd.DataFrame, frame_data, operator.methodcaller("eval", "A + 1")),
98-
marks=not_implemented_mark,
99-
),
93+
(pd.DataFrame, frame_data, operator.methodcaller("eval", "A + 1", engine="python")),
10094
(pd.DataFrame, frame_data, operator.methodcaller("select_dtypes", include="int")),
10195
(pd.DataFrame, frame_data, operator.methodcaller("assign", b=1)),
10296
(pd.DataFrame, frame_data, operator.methodcaller("set_axis", ["A"])),
@@ -289,10 +283,7 @@
289283
),
290284
(pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)),
291285
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
292-
pytest.param(
293-
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
294-
marks=not_implemented_mark,
295-
),
286+
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
296287
pytest.param(
297288
(pd.DataFrame, frame_data, operator.methodcaller("squeeze")),
298289
marks=not_implemented_mark,
@@ -317,10 +308,7 @@
317308
(pd.DataFrame, frame_data, operator.methodcaller("take", [0, 0])),
318309
(pd.DataFrame, frame_mi_data, operator.methodcaller("xs", "a")),
319310
(pd.Series, (1, mi), operator.methodcaller("xs", "a")),
320-
pytest.param(
321-
(pd.DataFrame, frame_data, operator.methodcaller("get", "A")),
322-
marks=not_implemented_mark,
323-
),
311+
(pd.DataFrame, frame_data, operator.methodcaller("get", "A")),
324312
(
325313
pd.DataFrame,
326314
frame_data,
@@ -532,6 +520,15 @@ def test_finalize_called(ndframe_method):
532520
assert result.attrs == {"a": 1}
533521

534522

523+
@not_implemented_mark
524+
def test_finalize_called_eval_numexpr():
525+
pytest.importorskip("numexpr")
526+
df = pd.DataFrame({"A": [1, 2]})
527+
df.attrs["A"] = 1
528+
result = df.eval("A + 1", engine="numexpr")
529+
assert result.attrs == {"A": 1}
530+
531+
535532
# ----------------------------------------------------------------------------
536533
# Binary operations
537534

0 commit comments

Comments
 (0)