From 3f05de1e7c7d2a20674dc845d65cb3a2c0a0cc16 Mon Sep 17 00:00:00 2001 From: OlivierLuG Date: Sat, 13 Jun 2020 20:59:08 +0200 Subject: [PATCH 1/3] #TST #13346 added tests --- pandas/tests/scalar/period/test_period.py | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 702899f163e06..34f8d05419bf5 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -6,6 +6,7 @@ from pandas._libs.tslibs import iNaT, period as libperiod from pandas._libs.tslibs.ccalendar import DAYS, MONTHS +from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime from pandas._libs.tslibs.parsing import DateParseError from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG, IncompatibleFrequency from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz @@ -767,6 +768,34 @@ def test_period_deprecated_freq(self): assert isinstance(p1, Period) assert isinstance(p2, Period) + @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)]) + def test_start_time_and_end_time_bounds(self, bound, offset): + # GH #13346 + period = Period( + year=bound.year, + month=bound.month, + day=bound.day, + hour=bound.hour, + minute=bound.minute, + second=bound.second - offset, + freq="us", + ) + period.start_time + period.end_time + period = Period( + year=bound.year, + month=bound.month, + day=bound.day, + hour=bound.hour, + minute=bound.minute, + second=bound.second + offset, + freq="us", + ) + with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): + period.start_time + with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): + period.end_time + def test_start_time(self): freq_lst = ["A", "Q", "M", "D", "H", "T", "S"] xp = datetime(2012, 1, 1) From 3c33ec8f8efcbd184e32883caf99c5291c8baf77 Mon Sep 17 00:00:00 2001 From: OlivierLuG Date: Sun, 14 Jun 2020 20:56:19 +0200 Subject: [PATCH 2/3] TST #13346 taken review into account --- pandas/tests/scalar/period/test_period.py | 36 +++++++++++++---------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 34f8d05419bf5..b26e5b3a5669f 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -768,21 +768,8 @@ def test_period_deprecated_freq(self): assert isinstance(p1, Period) assert isinstance(p2, Period) - @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)]) - def test_start_time_and_end_time_bounds(self, bound, offset): - # GH #13346 - period = Period( - year=bound.year, - month=bound.month, - day=bound.day, - hour=bound.hour, - minute=bound.minute, - second=bound.second - offset, - freq="us", - ) - period.start_time - period.end_time - period = Period( + def period_constructor(bound, offset): + return Period( year=bound.year, month=bound.month, day=bound.day, @@ -791,6 +778,25 @@ def test_start_time_and_end_time_bounds(self, bound, offset): second=bound.second + offset, freq="us", ) + + @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)]) + def test_start_time_and_end_time_bounds(self, bound, offset): + # GH #13346 + period = TestPeriodProperties.period_constructor(bound, -offset) + assert period.start_time.round(freq="S") == period.to_timestamp().round( + freq="S" + ) + assert period.end_time.round(freq="S") == period.to_timestamp().round(freq="S") + + result = period.start_time.floor("S") + expected = (bound - offset * Timedelta(1, unit="S")).floor("S") + assert result == expected + + result = period.end_time.floor("S") + expected = (bound - offset * Timedelta(1, unit="S")).floor("S") + assert result == expected + + period = TestPeriodProperties.period_constructor(bound, offset) with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): period.start_time with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): From 37f03d64bfde9b8d9efdd39e8e5fda308658584f Mon Sep 17 00:00:00 2001 From: OlivierLuG Date: Tue, 7 Jul 2020 21:52:04 +0200 Subject: [PATCH 3/3] Added tests for #13346 - with review --- pandas/tests/scalar/period/test_period.py | 33 ++++++++++------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index e7447f0989501..a235f27d48df8 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -770,7 +770,7 @@ def test_period_deprecated_freq(self): assert isinstance(p1, Period) assert isinstance(p2, Period) - def period_constructor(bound, offset): + def _period_constructor(bound, offset): return Period( year=bound.year, month=bound.month, @@ -782,27 +782,22 @@ def period_constructor(bound, offset): ) @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)]) - def test_start_time_and_end_time_bounds(self, bound, offset): + @pytest.mark.parametrize("period_property", ["start_time", "end_time"]) + def test_outter_bounds_start_and_end_time(self, bound, offset, period_property): # GH #13346 - period = TestPeriodProperties.period_constructor(bound, -offset) - assert period.start_time.round(freq="S") == period.to_timestamp().round( - freq="S" - ) - assert period.end_time.round(freq="S") == period.to_timestamp().round(freq="S") - - result = period.start_time.floor("S") - expected = (bound - offset * Timedelta(1, unit="S")).floor("S") - assert result == expected + period = TestPeriodProperties._period_constructor(bound, offset) + with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): + getattr(period, period_property) - result = period.end_time.floor("S") + @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)]) + @pytest.mark.parametrize("period_property", ["start_time", "end_time"]) + def test_inner_bounds_start_and_end_time(self, bound, offset, period_property): + # GH #13346 + period = TestPeriodProperties._period_constructor(bound, -offset) + expected = period.to_timestamp().round(freq="S") + assert getattr(period, period_property).round(freq="S") == expected expected = (bound - offset * Timedelta(1, unit="S")).floor("S") - assert result == expected - - period = TestPeriodProperties.period_constructor(bound, offset) - with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): - period.start_time - with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"): - period.end_time + assert getattr(period, period_property).floor("S") == expected def test_start_time(self): freq_lst = ["A", "Q", "M", "D", "H", "T", "S"]