From 652bfa5e0d1d537ba0cc130d4ee053bb0ac3901b Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 3 Jun 2022 09:58:47 -0700 Subject: [PATCH 1/3] BUG: PeriodArray.__add__(array_of_ints) --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/arrays/datetimelike.py | 4 ++-- pandas/tests/arithmetic/test_period.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 45c32d689bd5b..6f55ec740d655 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 a an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`??`) - 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..d64968245743b 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -914,6 +914,18 @@ 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): + # 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 From 048e3434e7274789e9cd1dfaa9f3713acfd2fac4 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 3 Jun 2022 09:59:55 -0700 Subject: [PATCH 2/3] GH ref --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/tests/arithmetic/test_period.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 6f55ec740d655..8e1ef81d1b04f 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -829,7 +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 a an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`??`) +- Bug in adding an array of integers to a an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`47209`) - Plotting diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index d64968245743b..7adc407fd5de1 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -915,7 +915,8 @@ def test_pi_sub_intlike(self, five): tm.assert_index_equal(result, exp) def test_pi_add_sub_int_array_freqn_gt1(self): - # test adding array of ints when freq.n > 1 matches scalar behavior + # 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 From c608b381109da8a071bfc3bd726158f224a3df70 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 3 Jun 2022 14:35:20 -0700 Subject: [PATCH 3/3] Update doc/source/whatsnew/v1.5.0.rst Co-authored-by: Matthew Roeschke --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 8e1ef81d1b04f..7cf7267c3d6b9 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -829,7 +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 a an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`47209`) +- Bug in adding an array of integers to an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`47209`) - Plotting