diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 45c32d689bd5b..7cf7267c3d6b9 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -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 diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index a8772709935af..96090904b829f 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -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 @@ -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 diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index c31556064eece..7adc407fd5de1 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -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