Skip to content

BUG: Index[offset_objects] + Period #50282

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 1 commit into from
Dec 19, 2022
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ I/O
Period
^^^^^^
- Bug in :meth:`Period.strftime` and :meth:`PeriodIndex.strftime`, raising ``UnicodeDecodeError`` when a locale-specific directive was passed (:issue:`46319`)
- Bug in adding a :class:`Period` object to an array of :class:`DateOffset` objects incorrectly raising ``TypeError`` (:issue:`50162`)
-

Plotting
Expand Down
16 changes: 16 additions & 0 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,11 @@ cdef class _Period(PeriodMixin):
raise TypeError(f"unsupported operand type(s) for +: '{sname}' "
f"and '{oname}'")

elif util.is_array(other):
if other.dtype == object:
# GH#50162
return np.array([self + x for x in other], dtype=object)

return NotImplemented

def __radd__(self, other):
Expand All @@ -1767,11 +1772,22 @@ cdef class _Period(PeriodMixin):
elif other is NaT:
return NaT

elif util.is_array(other):
if other.dtype == object:
# GH#50162
return np.array([self - x for x in other], dtype=object)

return NotImplemented

def __rsub__(self, other):
if other is NaT:
return NaT

elif util.is_array(other):
if other.dtype == object:
# GH#50162
return np.array([x - self for x in other], dtype=object)

return NotImplemented

def asfreq(self, freq, how="E") -> "Period":
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/arithmetic/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ def test_more_na_comparisons(self, dtype):


class TestArithmetic:
def test_add_period_to_array_of_offset(self):
# GH#50162
per = pd.Period("2012-1-1", freq="D")
pi = pd.period_range("2012-1-1", periods=10, freq="D")
idx = per - pi

expected = pd.Index([x + per for x in idx], dtype=object)
result = idx + per
tm.assert_index_equal(result, expected)

result = per + idx
tm.assert_index_equal(result, expected)

# TODO: parametrize
def test_pow_ops_object(self):
Expand Down