Skip to content
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

ENH: added finalize to binary operators on DataFrame, GH28283 #48551

Merged
merged 5 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ Metadata
^^^^^^^^
- Fixed metadata propagation in :meth:`DataFrame.melt` (:issue:`28283`)
- Fixed metadata propagation in :meth:`DataFrame.explode` (:issue:`28283`)
- Fixed metadata propagation for binary operators on :class:`DataFrame` (:issue:`28283`)
Copy link
Member

Choose a reason for hiding this comment

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

Could you move this to v1.6.0.rst?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved it to v1.6.0.rst. Wasn't exactly sure where to put it, please let me know if it's ok.

-

Other
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7725,7 +7725,7 @@ def _construct_result(self, result) -> DataFrame:
-------
DataFrame
"""
out = self._constructor(result, copy=False)
out = self._constructor(result, copy=False).__finalize__(self)
# Pin columns instead of passing to constructor for compat with
# non-unique columns case
out.columns = self.columns
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 @@ -68,9 +68,7 @@ def test_to_frame(self):
assert ser.to_frame().flags.allows_duplicate_labels is False

@pytest.mark.parametrize("func", ["add", "sub"])
@pytest.mark.parametrize(
"frame", [False, pytest.param(True, marks=not_implemented)]
)
@pytest.mark.parametrize("frame", [False, True])
@pytest.mark.parametrize("other", [1, pd.Series([1, 2], name="A")])
def test_binops(self, func, other, frame):
df = pd.Series([1, 2], name="A", index=["a", "b"]).set_flags(
Expand Down
20 changes: 7 additions & 13 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,10 @@
(pd.DataFrame, frame_data, operator.methodcaller("nlargest", 1, "A")),
(pd.DataFrame, frame_data, operator.methodcaller("nsmallest", 1, "A")),
(pd.DataFrame, frame_mi_data, operator.methodcaller("swaplevel")),
pytest.param(
(
pd.DataFrame,
frame_data,
operator.methodcaller("add", pd.DataFrame(*frame_data)),
),
marks=not_implemented_mark,
(
pd.DataFrame,
frame_data,
operator.methodcaller("add", pd.DataFrame(*frame_data)),
),
# TODO: div, mul, etc.
pytest.param(
Expand Down Expand Up @@ -539,21 +536,18 @@ def test_finalize_called_eval_numexpr():
(pd.DataFrame({"A": [1]}), pd.Series([1])),
],
)
def test_binops(request, args, annotate, all_arithmetic_functions):
# This generates 326 tests... Is that needed?
def test_binops(request, args, annotate, all_binary_operators):
# This generates 624 tests... Is that needed?
left, right = args
if annotate == "both" and isinstance(left, int) or isinstance(right, int):
return

if isinstance(left, pd.DataFrame) or isinstance(right, pd.DataFrame):
request.node.add_marker(pytest.mark.xfail(reason="not implemented"))

if annotate in {"left", "both"} and not isinstance(left, int):
left.attrs = {"a": 1}
if annotate in {"left", "both"} and not isinstance(right, int):
right.attrs = {"a": 1}

result = all_arithmetic_functions(left, right)
result = all_binary_operators(left, right)
assert result.attrs == {"a": 1}


Expand Down