Skip to content

Commit df8acf4

Browse files
BUG: PeriodArray.__add__(array_of_ints) (#47209)
* BUG: PeriodArray.__add__(array_of_ints) * GH ref * Update doc/source/whatsnew/v1.5.0.rst Co-authored-by: Matthew Roeschke <emailformattr@gmail.com> Co-authored-by: Matthew Roeschke <emailformattr@gmail.com>
1 parent b1b788f commit df8acf4

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ Period
829829
- Bug in inferring an incorrect ``freq`` when passing a string to :class:`Period` microseconds that are a multiple of 1000 (:issue:`46811`)
830830
- 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`)
831831
- 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`)
832+
- Bug in adding an array of integers to an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`47209`)
832833
-
833834

834835
Plotting

pandas/core/arrays/datetimelike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ def __add__(self, other):
13171317
if not is_period_dtype(self.dtype):
13181318
raise integer_op_not_supported(self)
13191319
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
1320-
other, operator.add
1320+
other * self.freq.n, operator.add
13211321
)
13221322
else:
13231323
# Includes Categorical, other ExtensionArrays
@@ -1379,7 +1379,7 @@ def __sub__(self, other):
13791379
if not is_period_dtype(self.dtype):
13801380
raise integer_op_not_supported(self)
13811381
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
1382-
other, operator.sub
1382+
other * self.freq.n, operator.sub
13831383
)
13841384
else:
13851385
# Includes ExtensionArrays, float_dtype

pandas/tests/arithmetic/test_period.py

+13
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,19 @@ def test_pi_sub_intlike(self, five):
914914
exp = rng + (-five)
915915
tm.assert_index_equal(result, exp)
916916

917+
def test_pi_add_sub_int_array_freqn_gt1(self):
918+
# GH#47209 test adding array of ints when freq.n > 1 matches
919+
# scalar behavior
920+
pi = period_range("2016-01-01", periods=10, freq="2D")
921+
arr = np.arange(10)
922+
result = pi + arr
923+
expected = pd.Index([x + y for x, y in zip(pi, arr)])
924+
tm.assert_index_equal(result, expected)
925+
926+
result = pi - arr
927+
expected = pd.Index([x - y for x, y in zip(pi, arr)])
928+
tm.assert_index_equal(result, expected)
929+
917930
def test_pi_sub_isub_offset(self):
918931
# offset
919932
# DateOffset

0 commit comments

Comments
 (0)