Skip to content

BUG: PeriodArray.__add__(array_of_ints) #47209

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 3 commits into from
Jun 3, 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/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ Period
- Bug in inferring an incorrect ``freq`` when passing a string to :class:`Period` microseconds that are a multiple of 1000 (:issue:`46811`)
- Bug in constructing a :class:`Period` from a :class:`Timestamp` or ``np.datetime64`` object with non-zero nanoseconds and ``freq="ns"`` incorrectly truncating the nanoseconds (:issue:`46811`)
- Bug in adding ``np.timedelta64("NaT", "ns")`` to a :class:`Period` with a timedelta-like freq incorrectly raising ``IncompatibleFrequency`` instead of returning ``NaT`` (:issue:`47196`)
- Bug in adding an array of integers to an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`47209`)
-

Plotting
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 @@ -1317,7 +1317,7 @@ def __add__(self, other):
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other, operator.add
other * self.freq.n, operator.add
)
else:
# Includes Categorical, other ExtensionArrays
Expand Down Expand Up @@ -1379,7 +1379,7 @@ def __sub__(self, other):
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other, operator.sub
other * self.freq.n, operator.sub
)
else:
# Includes ExtensionArrays, float_dtype
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,19 @@ def test_pi_sub_intlike(self, five):
exp = rng + (-five)
tm.assert_index_equal(result, exp)

def test_pi_add_sub_int_array_freqn_gt1(self):
# GH#47209 test adding array of ints when freq.n > 1 matches
# scalar behavior
pi = period_range("2016-01-01", periods=10, freq="2D")
arr = np.arange(10)
result = pi + arr
expected = pd.Index([x + y for x, y in zip(pi, arr)])
tm.assert_index_equal(result, expected)

result = pi - arr
expected = pd.Index([x - y for x, y in zip(pi, arr)])
tm.assert_index_equal(result, expected)

def test_pi_sub_isub_offset(self):
# offset
# DateOffset
Expand Down