Skip to content

Commit 5237be5

Browse files
authored
BUG: Index[offset_objects] + Period (#50282)
1 parent 6cbd87b commit 5237be5

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ I/O
895895
Period
896896
^^^^^^
897897
- Bug in :meth:`Period.strftime` and :meth:`PeriodIndex.strftime`, raising ``UnicodeDecodeError`` when a locale-specific directive was passed (:issue:`46319`)
898+
- Bug in adding a :class:`Period` object to an array of :class:`DateOffset` objects incorrectly raising ``TypeError`` (:issue:`50162`)
898899
-
899900

900901
Plotting

pandas/_libs/tslibs/period.pyx

+16
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,11 @@ cdef class _Period(PeriodMixin):
17411741
raise TypeError(f"unsupported operand type(s) for +: '{sname}' "
17421742
f"and '{oname}'")
17431743

1744+
elif util.is_array(other):
1745+
if other.dtype == object:
1746+
# GH#50162
1747+
return np.array([self + x for x in other], dtype=object)
1748+
17441749
return NotImplemented
17451750

17461751
def __radd__(self, other):
@@ -1767,11 +1772,22 @@ cdef class _Period(PeriodMixin):
17671772
elif other is NaT:
17681773
return NaT
17691774

1775+
elif util.is_array(other):
1776+
if other.dtype == object:
1777+
# GH#50162
1778+
return np.array([self - x for x in other], dtype=object)
1779+
17701780
return NotImplemented
17711781

17721782
def __rsub__(self, other):
17731783
if other is NaT:
17741784
return NaT
1785+
1786+
elif util.is_array(other):
1787+
if other.dtype == object:
1788+
# GH#50162
1789+
return np.array([x - self for x in other], dtype=object)
1790+
17751791
return NotImplemented
17761792

17771793
def asfreq(self, freq, how="E") -> "Period":

pandas/tests/arithmetic/test_object.py

+12
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ def test_more_na_comparisons(self, dtype):
7373

7474

7575
class TestArithmetic:
76+
def test_add_period_to_array_of_offset(self):
77+
# GH#50162
78+
per = pd.Period("2012-1-1", freq="D")
79+
pi = pd.period_range("2012-1-1", periods=10, freq="D")
80+
idx = per - pi
81+
82+
expected = pd.Index([x + per for x in idx], dtype=object)
83+
result = idx + per
84+
tm.assert_index_equal(result, expected)
85+
86+
result = per + idx
87+
tm.assert_index_equal(result, expected)
7688

7789
# TODO: parametrize
7890
def test_pow_ops_object(self):

0 commit comments

Comments
 (0)