Skip to content

Commit 1b66adf

Browse files
authored
BUG: td64+offset (#44532)
1 parent 2b8d566 commit 1b66adf

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ Datetimelike
544544
- Bug in in calling ``np.isnan``, ``np.isfinite``, or ``np.isinf`` on a timezone-aware :class:`DatetimeIndex` incorrectly raising ``TypeError`` (:issue:`43917`)
545545
- Bug in constructing a :class:`Series` from datetime-like strings with mixed timezones incorrectly partially-inferring datetime values (:issue:`40111`)
546546
- Bug in addition with a :class:`Tick` object and a ``np.timedelta64`` object incorrectly raising instead of returning :class:`Timedelta` (:issue:`44474`)
547+
- Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`)
548+
-
547549

548550
Timedelta
549551
^^^^^^^^^

pandas/_libs/tslibs/offsets.pyx

+9-3
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ cdef class BaseOffset:
353353
"""
354354
Base class for DateOffset methods that are not overridden by subclasses.
355355
"""
356+
# ensure that reversed-ops with numpy scalars return NotImplemented
357+
__array_priority__ = 1000
358+
356359
_day_opt = None
357360
_attributes = tuple(["n", "normalize"])
358361
_use_relativedelta = False
@@ -434,6 +437,10 @@ cdef class BaseOffset:
434437
if not isinstance(self, BaseOffset):
435438
# cython semantics; this is __radd__
436439
return other.__add__(self)
440+
441+
elif util.is_array(other) and other.dtype == object:
442+
return np.array([self + x for x in other])
443+
437444
try:
438445
return self.apply(other)
439446
except ApplyTypeError:
@@ -448,7 +455,8 @@ cdef class BaseOffset:
448455
elif not isinstance(self, BaseOffset):
449456
# cython semantics, this is __rsub__
450457
return (-other).__add__(self)
451-
else: # pragma: no cover
458+
else:
459+
# e.g. PeriodIndex
452460
return NotImplemented
453461

454462
def __call__(self, other):
@@ -767,8 +775,6 @@ cdef class SingleConstructorOffset(BaseOffset):
767775
# Tick Offsets
768776

769777
cdef class Tick(SingleConstructorOffset):
770-
# ensure that reversed-ops with numpy scalars return NotImplemented
771-
__array_priority__ = 1000
772778
_adjust_dst = False
773779
_prefix = "undefined"
774780
_attributes = tuple(["n", "normalize"])

pandas/tests/tseries/offsets/test_business_day.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
timedelta,
88
)
99

10-
import numpy as np
1110
import pytest
1211

1312
from pandas._libs.tslibs.offsets import (
@@ -61,7 +60,6 @@ def test_with_offset(self):
6160

6261
assert (self.d + offset) == datetime(2008, 1, 2, 2)
6362

64-
@pytest.mark.parametrize("reverse", [True, False])
6563
@pytest.mark.parametrize(
6664
"td",
6765
[
@@ -71,20 +69,15 @@ def test_with_offset(self):
7169
],
7270
ids=lambda x: type(x),
7371
)
74-
def test_with_offset_index(self, reverse, td, request):
75-
if reverse and isinstance(td, np.timedelta64):
76-
mark = pytest.mark.xfail(
77-
reason="need __array_priority__, but that causes other errors"
78-
)
79-
request.node.add_marker(mark)
72+
def test_with_offset_index(self, td):
8073

8174
dti = DatetimeIndex([self.d])
8275
expected = DatetimeIndex([datetime(2008, 1, 2, 2)])
8376

84-
if reverse:
85-
result = dti + (td + self.offset)
86-
else:
87-
result = dti + (self.offset + td)
77+
result = dti + (td + self.offset)
78+
tm.assert_index_equal(result, expected)
79+
80+
result = dti + (self.offset + td)
8881
tm.assert_index_equal(result, expected)
8982

9083
def test_eq(self):

0 commit comments

Comments
 (0)