Skip to content

Commit

Permalink
ENH: added finalize to binary operators on DataFrame, GH28283 (pandas…
Browse files Browse the repository at this point in the history
…-dev#48551)

* ENH/TST/DOC: Added finalize to DataFrame binary ops, GH28283

* ENH/TST/DOC: Added finalize to DataFrame binary ops, GH28283

* TST: fix not implemented tests

* DOC: moved mention to whatsnew 1.6
  • Loading branch information
seljaks authored and noatamir committed Nov 9, 2022
1 parent 7b33cf0 commit e758672
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Other enhancements
- :meth:`Series.add_suffix`, :meth:`DataFrame.add_suffix`, :meth:`Series.add_prefix` and :meth:`DataFrame.add_prefix` support an ``axis`` argument. If ``axis`` is set, the default behaviour of which axis to consider can be overwritten (:issue:`47819`)
- :func:`assert_frame_equal` now shows the first element where the DataFrames differ, analogously to ``pytest``'s output (:issue:`47910`)
- Added ``index`` parameter to :meth:`DataFrame.to_dict` (:issue:`46398`)
- Added metadata propagation for binary operators on :class:`DataFrame` (:issue:`28283`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7711,7 +7711,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

0 comments on commit e758672

Please sign in to comment.