From 7d8f064e3bf33bb0b8ec99f7be70a5d19c1a82d2 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 28 Jun 2022 14:50:40 -0700 Subject: [PATCH 1/2] BUG: PeriodArray.__sub__(Period) overflows --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/arrays/datetimelike.py | 5 +++-- pandas/tests/arrays/test_period.py | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 452b4259d4460..9758d84bbaf64 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -904,6 +904,7 @@ Period - 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`) +- Bug in subtracting a :class:`Period` from an array with :class:`PeriodDtype` returning incorrect results instead of raising ``OverflowError`` when the operation overflows (:issue:`??`) - Plotting diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 5e65b124ae0f4..d3c4815af5759 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1182,8 +1182,9 @@ def _sub_period(self, other: Period) -> npt.NDArray[np.object_]: # If the operation is well-defined, we return an object-dtype ndarray # of DateOffsets. Null entries are filled with pd.NaT self._check_compatible_with(other) - asi8 = self.asi8 - new_i8_data = asi8 - other.ordinal # TODO: checked_add_with_arr + new_i8_data = checked_add_with_arr( + self.asi8, -other.ordinal, arr_mask=self._isnan + ) new_data = np.array([self.freq.base * x for x in new_i8_data]) if self._hasna: diff --git a/pandas/tests/arrays/test_period.py b/pandas/tests/arrays/test_period.py index de0e766e4a2aa..78d2111971049 100644 --- a/pandas/tests/arrays/test_period.py +++ b/pandas/tests/arrays/test_period.py @@ -115,6 +115,19 @@ def test_sub_period(): arr - other +def test_sub_period_overflow(): + dti = pd.date_range("1677-09-22", periods=2, freq="D") + pi = dti.to_period("ns") + + per = pd.Period._from_ordinal(10**14, pi.freq) + + with pytest.raises(OverflowError, match="Overflow in int64 addition"): + pi - per + + with pytest.raises(OverflowError, match="Overflow in int64 addition"): + per - pi + + # ---------------------------------------------------------------------------- # Methods From 02e200f1b028adab74511c96c3f5746f00db0b8e Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 28 Jun 2022 14:51:50 -0700 Subject: [PATCH 2/2] GH refs --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/tests/arrays/test_period.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 9758d84bbaf64..40f13726b90b8 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -904,7 +904,7 @@ Period - 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`) -- Bug in subtracting a :class:`Period` from an array with :class:`PeriodDtype` returning incorrect results instead of raising ``OverflowError`` when the operation overflows (:issue:`??`) +- Bug in subtracting a :class:`Period` from an array with :class:`PeriodDtype` returning incorrect results instead of raising ``OverflowError`` when the operation overflows (:issue:`47538`) - Plotting diff --git a/pandas/tests/arrays/test_period.py b/pandas/tests/arrays/test_period.py index 78d2111971049..a4b442ff526e9 100644 --- a/pandas/tests/arrays/test_period.py +++ b/pandas/tests/arrays/test_period.py @@ -116,6 +116,7 @@ def test_sub_period(): def test_sub_period_overflow(): + # GH#47538 dti = pd.date_range("1677-09-22", periods=2, freq="D") pi = dti.to_period("ns")