Skip to content

Call finalize in Series.__array_ufunc__ #36553

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -434,6 +434,7 @@ Other
- Fixed metadata propagation in the :class:`Series.dt` accessor (:issue:`28283`)
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)
- Fixed metadata propagation in :meth:`Series.abs` and ufuncs called on Series (:issue:`28283`)

.. ---------------------------------------------------------------------------

Expand Down
8 changes: 7 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,13 @@ def construct_return(result):
# GH#27198
raise NotImplementedError
return result
return self._constructor(result, index=index, name=name, copy=False)
# TODO: When we support multiple values in __finalize__, this
# should pass alignable as `other` instead of self.
# Then `np.add(a, b)` would consider attrs from both a and b
# when a and b are NDFrames.
return self._constructor(
result, index=index, name=name, copy=False
).__finalize__(self)

if type(result) is tuple:
# multiple return values
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import operator
import re
from typing import Any, List, Tuple

import numpy as np
import pytest
Expand All @@ -29,7 +30,7 @@
# - Tuple: Constructor args
# - Callable: pass the constructed value with attrs set to this.

_all_methods = [
_all_methods: List[Tuple[Any, Any, Any]] = [
(
pd.Series,
(np.array([0], dtype="float64")),
Expand Down Expand Up @@ -330,7 +331,8 @@
(pd.DataFrame, frame_data, operator.inv),
(pd.Series, [1], operator.inv),
(pd.DataFrame, frame_data, abs),
pytest.param((pd.Series, [1], abs), marks=not_implemented_mark),
(pd.Series, [1], abs),
(pd.Series, [1], np.arccos),
pytest.param((pd.DataFrame, frame_data, round), marks=not_implemented_mark),
(pd.Series, [1], round),
(pd.DataFrame, frame_data, operator.methodcaller("take", [0, 0])),
Expand Down