Skip to content

Commit f3fbd0f

Browse files
authored
BUG: np.isinf, np.isfinite, np.isnan with DTI[dt64tz] (pandas-dev#43917)
1 parent c5eea60 commit f3fbd0f

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ Datetimelike
391391
- :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`)
392392
- 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`)
393393
- Bug in inplace addition and subtraction of :class:`DatetimeIndex` or :class:`TimedeltaIndex` with :class:`DatetimeArray` or :class:`TimedeltaArray` (:issue:`43904`)
394+
- Bug in in calling ``np.isnan``, ``np.isfinite``, or ``np.isinf`` on a timezone-aware :class:`DatetimeIndex` incorrectly raising ``TypeError`` (:issue:`43917`)
394395
-
395396

396397
Timedelta

pandas/core/arrays/datetimelike.py

+11
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,17 @@ class TimelikeOps(DatetimeLikeArrayMixin):
17031703
Common ops for TimedeltaIndex/DatetimeIndex, but not PeriodIndex.
17041704
"""
17051705

1706+
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
1707+
if (
1708+
ufunc in [np.isnan, np.isinf, np.isfinite]
1709+
and len(inputs) == 1
1710+
and inputs[0] is self
1711+
):
1712+
# numpy 1.18 changed isinf and isnan to not raise on dt64/td64
1713+
return getattr(ufunc, method)(self._ndarray, **kwargs)
1714+
1715+
return super().__array_ufunc__(ufunc, method, *inputs, **kwargs)
1716+
17061717
def _round(self, freq, mode, ambiguous, nonexistent):
17071718
# round the local times
17081719
if is_datetime64tz_dtype(self.dtype):

pandas/tests/indexes/test_numpy_compat.py

-7
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ def test_numpy_ufuncs_other(index, func, request):
7474
# test ufuncs of numpy, see:
7575
# https://numpy.org/doc/stable/reference/ufuncs.html
7676
if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
77-
if (
78-
isinstance(index, DatetimeIndex)
79-
and index.tz is not None
80-
and func in [np.isfinite, np.isnan, np.isinf]
81-
):
82-
mark = pytest.mark.xfail(reason="__array_ufunc__ is not defined")
83-
request.node.add_marker(mark)
8477

8578
if func in (np.isfinite, np.isinf, np.isnan):
8679
# numpy 1.18 changed isinf and isnan to not raise on dt64/td64

0 commit comments

Comments
 (0)