Skip to content

Commit 2b1184d

Browse files
authored
BUG: PeriodArray.__sub__(Period) overflows (#47538)
* BUG: PeriodArray.__sub__(Period) overflows * GH refs
1 parent 80c005e commit 2b1184d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ Period
905905
- 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`)
906906
- 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`)
907907
- Bug in adding an array of integers to an array with :class:`PeriodDtype` giving incorrect results when ``dtype.freq.n > 1`` (:issue:`47209`)
908+
- 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`)
908909
-
909910

910911
Plotting

pandas/core/arrays/datetimelike.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,9 @@ def _sub_period(self, other: Period) -> npt.NDArray[np.object_]:
11821182
# If the operation is well-defined, we return an object-dtype ndarray
11831183
# of DateOffsets. Null entries are filled with pd.NaT
11841184
self._check_compatible_with(other)
1185-
asi8 = self.asi8
1186-
new_i8_data = asi8 - other.ordinal # TODO: checked_add_with_arr
1185+
new_i8_data = checked_add_with_arr(
1186+
self.asi8, -other.ordinal, arr_mask=self._isnan
1187+
)
11871188
new_data = np.array([self.freq.base * x for x in new_i8_data])
11881189

11891190
if self._hasna:

pandas/tests/arrays/test_period.py

+14
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ def test_sub_period():
115115
arr - other
116116

117117

118+
def test_sub_period_overflow():
119+
# GH#47538
120+
dti = pd.date_range("1677-09-22", periods=2, freq="D")
121+
pi = dti.to_period("ns")
122+
123+
per = pd.Period._from_ordinal(10**14, pi.freq)
124+
125+
with pytest.raises(OverflowError, match="Overflow in int64 addition"):
126+
pi - per
127+
128+
with pytest.raises(OverflowError, match="Overflow in int64 addition"):
129+
per - pi
130+
131+
118132
# ----------------------------------------------------------------------------
119133
# Methods
120134

0 commit comments

Comments
 (0)