Skip to content

Commit 5541834

Browse files
authored
TST: Period with Timestamp overflow (#34755)
* #TST #13346 added tests * TST #13346 taken review into account * Added tests for #13346 - with review
1 parent b9715d4 commit 5541834

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pandas/tests/scalar/period/test_period.py

+30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pandas._libs.tslibs import iNaT, period as libperiod
88
from pandas._libs.tslibs.ccalendar import DAYS, MONTHS
9+
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
910
from pandas._libs.tslibs.parsing import DateParseError
1011
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG, IncompatibleFrequency
1112
from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz
@@ -776,6 +777,35 @@ def test_period_deprecated_freq(self):
776777
assert isinstance(p1, Period)
777778
assert isinstance(p2, Period)
778779

780+
def _period_constructor(bound, offset):
781+
return Period(
782+
year=bound.year,
783+
month=bound.month,
784+
day=bound.day,
785+
hour=bound.hour,
786+
minute=bound.minute,
787+
second=bound.second + offset,
788+
freq="us",
789+
)
790+
791+
@pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)])
792+
@pytest.mark.parametrize("period_property", ["start_time", "end_time"])
793+
def test_outter_bounds_start_and_end_time(self, bound, offset, period_property):
794+
# GH #13346
795+
period = TestPeriodProperties._period_constructor(bound, offset)
796+
with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"):
797+
getattr(period, period_property)
798+
799+
@pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)])
800+
@pytest.mark.parametrize("period_property", ["start_time", "end_time"])
801+
def test_inner_bounds_start_and_end_time(self, bound, offset, period_property):
802+
# GH #13346
803+
period = TestPeriodProperties._period_constructor(bound, -offset)
804+
expected = period.to_timestamp().round(freq="S")
805+
assert getattr(period, period_property).round(freq="S") == expected
806+
expected = (bound - offset * Timedelta(1, unit="S")).floor("S")
807+
assert getattr(period, period_property).floor("S") == expected
808+
779809
def test_start_time(self):
780810
freq_lst = ["A", "Q", "M", "D", "H", "T", "S"]
781811
xp = datetime(2012, 1, 1)

0 commit comments

Comments
 (0)