Skip to content

Commit 26b2e02

Browse files
committed
BUG: Raise OutOfBoundsDatetime when Period is constructed from invalid high-resolution Timestamp (#63278)
1 parent b6b0af1 commit 26b2e02

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

pandas/_libs/tslibs/period.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,9 @@ cdef int64_t _period_ordinal_safe(npy_datetimestruct *dts, int freq) except? -1:
849849
except OverflowError as err:
850850
# Translate low-level overflow into a user-facing OutOfBoundsDatetime.
851851
fmt = dts_to_iso_string(dts)
852-
raise OutOfBoundsDatetime(f"Out of bounds datetime for Period with freq {freq}: {fmt}") from err
852+
raise OutOfBoundsDatetime(
853+
f"Out of bounds datetime for Period with freq {freq}: {fmt}"
854+
) from err
853855

854856

855857
cdef void get_date_info(int64_t ordinal,
@@ -1189,7 +1191,7 @@ cpdef int64_t period_ordinal(int y, int m, int d, int h, int min,
11891191
dts.sec = s
11901192
dts.us = us
11911193
dts.ps = ps
1192-
#return get_period_ordinal(&dts, freq)
1194+
# return get_period_ordinal(&dts, freq)
11931195
return _period_ordinal_safe(&dts, freq)
11941196

11951197

pandas/tests/tslibs/test_period.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import numpy as np
22
import pytest
3-
import pandas as pd
3+
44
from pandas._libs.tslibs import (
5+
OutOfBoundsDatetime,
56
iNaT,
67
to_offset,
7-
OutOfBoundsDatetime,
88
)
99
from pandas._libs.tslibs.period import (
1010
extract_ordinals,
@@ -13,6 +13,7 @@
1313
period_ordinal,
1414
)
1515

16+
import pandas as pd
1617
import pandas._testing as tm
1718

1819

@@ -123,9 +124,10 @@ def test_get_period_field_array_raises_on_out_of_range():
123124
with pytest.raises(ValueError, match=msg):
124125
get_period_field_arr(-1, np.empty(1), 0)
125126

127+
126128
def test_period_from_overflow_timestamp_raises():
127129
# Construct a deliberately broken Timestamp
128130
ts = pd.Timestamp(pd.Timestamp.min.value, unit="us")
129131

130132
with pytest.raises(OutOfBoundsDatetime):
131-
pd.Period(ts, freq="us")
133+
pd.Period(ts, freq="us")

0 commit comments

Comments
 (0)