Skip to content

Fixed metadata propagation in DataFrame.__getitem__ #37037

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 6 commits into from
Nov 15, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ Other
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` incorrectly casting from ``PeriodDtype`` to object dtype (:issue:`34871`)
- Fixed bug in metadata propagation incorrectly copying DataFrame columns as metadata when the column name overlaps with the metadata name (:issue:`37037`)
- 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`)
- Fixed metadata propagation when selecting columns from a DataFrame with ``DataFrame.__getitem__`` (:issue:`28283`)
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)
- 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`)
- Bug in ``accessor.DirNamesMixin``, where ``dir(obj)`` wouldn't show attributes defined on the instance (:issue:`37173`).
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3777,7 +3777,7 @@ def _get_item_cache(self, item):

loc = self.columns.get_loc(item)
values = self._mgr.iget(loc)
res = self._box_col_values(values, loc)
res = self._box_col_values(values, loc).__finalize__(self)

cache[item] = res
res._set_as_cached(item, self)
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/generic/test_duplicate_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ def test_series_raises(self, func):
pytest.param(
operator.itemgetter(("a", ["A", "A"])), "loc", marks=not_implemented
),
pytest.param(
operator.itemgetter((["a", "a"], "A")), "loc", marks=not_implemented
),
(operator.itemgetter((["a", "a"], "A")), "loc"),
# iloc
(operator.itemgetter([0, 0]), "iloc"),
pytest.param(
Expand Down
29 changes: 13 additions & 16 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,12 @@
marks=pytest.mark.xfail(reason="Implement binary finalize"),
),
(pd.DataFrame, frame_data, operator.methodcaller("transpose")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", "A")),
marks=not_implemented_mark,
),
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", "A")),
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", ["A"])),
(pd.DataFrame, frame_data, operator.methodcaller("__getitem__", np.array([True]))),
(pd.DataFrame, ({("A", "a"): [1]},), operator.methodcaller("__getitem__", ["A"])),
(pd.DataFrame, frame_data, operator.methodcaller("query", "A == 1")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("eval", "A + 1")),
marks=not_implemented_mark,
),
(pd.DataFrame, frame_data, operator.methodcaller("eval", "A + 1", engine="python")),
(pd.DataFrame, frame_data, operator.methodcaller("select_dtypes", include="int")),
(pd.DataFrame, frame_data, operator.methodcaller("assign", b=1)),
(pd.DataFrame, frame_data, operator.methodcaller("set_axis", ["A"])),
Expand Down Expand Up @@ -289,10 +283,7 @@
),
(pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)),
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
marks=not_implemented_mark,
),
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("squeeze")),
marks=not_implemented_mark,
Expand All @@ -317,10 +308,7 @@
(pd.DataFrame, frame_data, operator.methodcaller("take", [0, 0])),
(pd.DataFrame, frame_mi_data, operator.methodcaller("xs", "a")),
(pd.Series, (1, mi), operator.methodcaller("xs", "a")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("get", "A")),
marks=not_implemented_mark,
),
(pd.DataFrame, frame_data, operator.methodcaller("get", "A")),
(
pd.DataFrame,
frame_data,
Expand Down Expand Up @@ -532,6 +520,15 @@ def test_finalize_called(ndframe_method):
assert result.attrs == {"a": 1}


@not_implemented_mark
def test_finalize_called_eval_numexpr():
pytest.importorskip("numexpr")
df = pd.DataFrame({"A": [1, 2]})
df.attrs["A"] = 1
result = df.eval("A + 1", engine="numexpr")
assert result.attrs == {"A": 1}


# ----------------------------------------------------------------------------
# Binary operations

Expand Down