Skip to content

ENH: implement Index.__array_ufunc__ #43904

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 9 commits into from
Oct 7, 2021
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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ Datetimelike
- Bug in :func:`to_datetime` with ``format`` and ``pandas.NA`` was raising ``ValueError`` (:issue:`42957`)
- :func:`to_datetime` would silently swap ``MM/DD/YYYY`` and ``DD/MM/YYYY`` formats if the given ``dayfirst`` option could not be respected - now, a warning is raised in the case of delimited date strings (e.g. ``31-12-2012``) (:issue:`12585`)
- Bug in :meth:`date_range` and :meth:`bdate_range` do not return right bound when ``start`` = ``end`` and set is closed on one side (:issue:`43394`)
- Bug in inplace addition and subtraction of :class:`DatetimeIndex` or :class:`TimedeltaIndex` with :class:`DatetimeArray` or :class:`TimedeltaArray` (:issue:`43904`)
-

Timedelta
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arraylike.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def reconstruct(result):
return result

if "out" in kwargs:
result = _dispatch_ufunc_with_out(self, ufunc, method, *inputs, **kwargs)
result = dispatch_ufunc_with_out(self, ufunc, method, *inputs, **kwargs)
return reconstruct(result)

# We still get here with kwargs `axis` for e.g. np.maximum.accumulate
Expand Down Expand Up @@ -410,7 +410,7 @@ def _standardize_out_kwarg(**kwargs) -> dict:
return kwargs


def _dispatch_ufunc_with_out(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
def dispatch_ufunc_with_out(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
"""
If we have an `out` keyword, then call the ufunc without `out` and then
set the result into the given `out`.
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,11 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
if result is not NotImplemented:
return result

if "out" in kwargs:
return arraylike.dispatch_ufunc_with_out(
self, ufunc, method, *inputs, **kwargs
)

return arraylike.default_array_ufunc(self, ufunc, method, *inputs, **kwargs)


Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ def __iadd__(self, other):

if not is_period_dtype(self.dtype):
# restore freq, which is invalidated by setitem
self._freq = result._freq
self._freq = result.freq
return self

def __isub__(self, other):
Expand All @@ -1423,7 +1423,7 @@ def __isub__(self, other):

if not is_period_dtype(self.dtype):
# restore freq, which is invalidated by setitem
self._freq = result._freq
self._freq = result.freq
return self

# --------------------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
PeriodDtype,
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeIndex,
ABCMultiIndex,
ABCPeriodIndex,
Expand All @@ -116,6 +117,7 @@
)

from pandas.core import (
arraylike,
missing,
ops,
)
Expand Down Expand Up @@ -844,6 +846,24 @@ def __array__(self, dtype=None) -> np.ndarray:
"""
return np.asarray(self._data, dtype=dtype)

def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
if any(isinstance(other, (ABCSeries, ABCDataFrame)) for other in inputs):
return NotImplemented

result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
self, ufunc, method, *inputs, **kwargs
)
if result is not NotImplemented:
return result

new_inputs = [x if x is not self else x._values for x in inputs]
result = getattr(ufunc, method)(*new_inputs, **kwargs)
if ufunc.nout == 2:
# i.e. np.divmod, np.modf, np.frexp
return tuple(self.__array_wrap__(x) for x in result)

return self.__array_wrap__(result)

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc and other functions.
Expand Down
9 changes: 0 additions & 9 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,15 +672,6 @@ def insert(self, loc: int, item):
# --------------------------------------------------------------------
# NDArray-Like Methods

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc and other functions.
"""
out = super().__array_wrap__(result, context=context)
if isinstance(out, DatetimeTimedeltaMixin) and self.freq is not None:
out = out._with_freq("infer")
return out

@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
nv.validate_take((), kwargs)
Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,15 @@ def test_dti_isub_tdi(self, tz_naive_fixture):
result -= tdi
tm.assert_index_equal(result, expected)

# DTA.__isub__ GH#43904
dta = dti._data.copy()
dta -= tdi
tm.assert_datetime_array_equal(dta, expected._data)

out = dti._data.copy()
np.subtract(out, tdi, out=out)
tm.assert_datetime_array_equal(out, expected._data)

msg = "cannot subtract .* from a TimedeltaArray"
with pytest.raises(TypeError, match=msg):
tdi -= dti
Expand All @@ -2172,10 +2181,14 @@ def test_dti_isub_tdi(self, tz_naive_fixture):
result -= tdi.values
tm.assert_index_equal(result, expected)

msg = "cannot subtract a datelike from a TimedeltaArray"
msg = "cannot subtract DatetimeArray from ndarray"
with pytest.raises(TypeError, match=msg):
tdi.values -= dti

msg = "cannot subtract a datelike from a TimedeltaArray"
with pytest.raises(TypeError, match=msg):
tdi._values -= dti

# -------------------------------------------------------------
# Binary Operations DatetimeIndex and datetime-like
# TODO: A couple other tests belong in this section. Move them in
Expand Down