Skip to content

Commit 6cf0735

Browse files
Backport PR #36535: Regr/period range large value/issue 36430 (#36572)
Co-authored-by: nrebena <[email protected]>
1 parent da2f3a8 commit 6cf0735

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Fixed regressions
3636
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`,:issue:`35802`)
3737
- Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`,:issue:`36377`)
3838
- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`)
39+
- Fixed regression in :class:`Period` incorrect value for ordinal over the maximum timestamp (:issue:`36430`)
3940

4041
.. ---------------------------------------------------------------------------
4142

pandas/_libs/tslibs/period.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ cdef int64_t get_time_nanos(int freq, int64_t unix_date, int64_t ordinal) nogil:
861861
"""
862862
cdef:
863863
int64_t sub, factor
864+
int64_t nanos_in_day = 24 * 3600 * 10**9
864865

865866
freq = get_freq_group(freq)
866867

@@ -886,7 +887,7 @@ cdef int64_t get_time_nanos(int freq, int64_t unix_date, int64_t ordinal) nogil:
886887
# We must have freq == FR_HR
887888
factor = 10**9 * 3600
888889

889-
sub = ordinal - unix_date * 24 * 3600 * 10**9 / factor
890+
sub = ordinal - unix_date * (nanos_in_day / factor)
890891
return sub * factor
891892

892893

pandas/tests/scalar/period/test_period.py

+7
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ def test_period_cons_combined(self):
486486
with pytest.raises(ValueError, match=msg):
487487
Period("2011-01", freq="1D1W")
488488

489+
@pytest.mark.parametrize("hour", range(24))
490+
def test_period_large_ordinal(self, hour):
491+
# Issue #36430
492+
# Integer overflow for Period over the maximum timestamp
493+
p = pd.Period(ordinal=2562048 + hour, freq="1H")
494+
assert p.hour == hour
495+
489496

490497
class TestPeriodMethods:
491498
def test_round_trip(self):

0 commit comments

Comments
 (0)