Skip to content

BUG: fix to_timestamp out_of_bounds #27916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- Bug in :func:`to_datetime` where passing a timezone-naive :class:`DatetimeArray` or :class:`DatetimeIndex` and ``utc=True`` would incorrectly return a timezone-naive result (:issue:`27733`)
-
- Bug in :meth:`Period.to_timestamp` where a :class:`Period` outside the :class:`Timestamp` implementation bounds (roughly 1677-09-21 to 2262-04-11) would return an incorrect :class:`Timestamp` instead of raising ``OutOfBoundsDatetime`` (:issue:`19643`)
-
-

Expand Down
13 changes: 7 additions & 6 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ PyDateTime_IMPORT

from pandas._libs.tslibs.np_datetime cimport (
npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct,
pandas_datetime_to_datetimestruct, NPY_DATETIMEUNIT, NPY_FR_D)
pandas_datetime_to_datetimestruct, check_dts_bounds,
NPY_DATETIMEUNIT, NPY_FR_D)

cdef extern from "src/datetime/np_datetime.h":
int64_t npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
Expand Down Expand Up @@ -1011,7 +1012,7 @@ def dt64arr_to_periodarr(int64_t[:] dtarr, int freq, tz=None):

@cython.wraparound(False)
@cython.boundscheck(False)
def periodarr_to_dt64arr(int64_t[:] periodarr, int freq):
def periodarr_to_dt64arr(const int64_t[:] periodarr, int freq):
"""
Convert array to datetime64 values from a set of ordinals corresponding to
periods per period convention.
Expand All @@ -1024,9 +1025,8 @@ def periodarr_to_dt64arr(int64_t[:] periodarr, int freq):

out = np.empty(l, dtype='i8')

with nogil:
for i in range(l):
out[i] = period_ordinal_to_dt64(periodarr[i], freq)
for i in range(l):
out[i] = period_ordinal_to_dt64(periodarr[i], freq)

return out.base # .base to access underlying np.ndarray

Expand Down Expand Up @@ -1179,14 +1179,15 @@ cpdef int64_t period_ordinal(int y, int m, int d, int h, int min,
return get_period_ordinal(&dts, freq)


cpdef int64_t period_ordinal_to_dt64(int64_t ordinal, int freq) nogil:
cdef int64_t period_ordinal_to_dt64(int64_t ordinal, int freq) except? -1:
cdef:
npy_datetimestruct dts

if ordinal == NPY_NAT:
return NPY_NAT

get_date_info(ordinal, freq, &dts)
check_dts_bounds(&dts)
return dtstruct_to_dt64(&dts)


Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas._libs import OutOfBoundsDatetime

import pandas as pd
from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray
import pandas.util.testing as tm
Expand Down Expand Up @@ -615,6 +617,15 @@ def test_to_timestamp(self, how, period_index):
# an EA-specific tm.assert_ function
tm.assert_index_equal(pd.Index(result), pd.Index(expected))

def test_to_timestamp_out_of_bounds(self):
# GH#19643 previously overflowed silently
pi = pd.period_range("1500", freq="Y", periods=3)
with pytest.raises(OutOfBoundsDatetime):
pi.to_timestamp()

with pytest.raises(OutOfBoundsDatetime):
pi._data.to_timestamp()

@pytest.mark.parametrize("propname", PeriodArray._bool_ops)
def test_bool_properties(self, period_index, propname):
# in this case _bool_ops is just `is_leap_year`
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/scalar/period/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ def test_asfreq_near_zero_weekly(self):
assert week1.asfreq("D", "E") >= per1
assert week2.asfreq("D", "S") <= per2

@pytest.mark.xfail(
reason="GH#19643 period_helper asfreq functions fail to check for overflows"
)
def test_to_timestamp_out_of_bounds(self):
# GH#19643, currently gives Timestamp('1754-08-30 22:43:41.128654848')
# GH#19643, used to incorrectly give Timestamp in 1754
per = Period("0001-01-01", freq="B")
with pytest.raises(OutOfBoundsDatetime):
per.to_timestamp()
Expand Down