Skip to content

BUG: Out of bounds Timestamp does not raise exception #19382

Closed
@cbertinato

Description

@cbertinato

An OutOfBoundsDatetime exception is raised if a datetime that goes beyond the minimum datetime is specified in both nanoseconds:

>>> Timestamp(-9223372036854775000)
Timestamp('1677-09-21 00:12:43.145225')
>>> Timestamp(-9223372036854775001)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 581, in pandas._libs.tslibs.timestamps.Timestamp.__new__
    ts = convert_to_tsobject(ts_input, tz, unit, 0, 0)
  File "pandas/_libs/tslibs/conversion.pyx", line 312, in pandas._libs.tslibs.conversion.convert_to_tsobject
    check_dts_bounds(&obj.dts)
  File "pandas/_libs/tslibs/np_datetime.pyx", line 121, in pandas._libs.tslibs.np_datetime.check_dts_bounds
    raise OutOfBoundsDatetime(
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1677-09-21 00:12:43

and as a string:

>>> Timestamp('1677-09-21 00:12:43.145225')
Timestamp('1677-09-21 00:12:43.145225')
>>> Timestamp('1677-09-21 00:12:43.145224')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 581, in pandas._libs.tslibs.timestamps.Timestamp.__new__
    ts = convert_to_tsobject(ts_input, tz, unit, 0, 0)
  File "pandas/_libs/tslibs/conversion.pyx", line 274, in pandas._libs.tslibs.conversion.convert_to_tsobject
    return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
  File "pandas/_libs/tslibs/conversion.pyx", line 482, in pandas._libs.tslibs.conversion.convert_str_to_tsobject
    return convert_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
  File "pandas/_libs/tslibs/conversion.pyx", line 299, in pandas._libs.tslibs.conversion.convert_to_tsobject
    return convert_datetime_to_tsobject(ts, tz)
  File "pandas/_libs/tslibs/conversion.pyx", line 392, in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject
    check_dts_bounds(&obj.dts)
  File "pandas/_libs/tslibs/np_datetime.pyx", line 121, in pandas._libs.tslibs.np_datetime.check_dts_bounds
    raise OutOfBoundsDatetime(
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1677-09-21 00:12:43

An OverflowError is raised if going beyond the maximum datetime when instantiating with the number nanoseconds:

>>> Timestamp(9223372036854775807)
Timestamp('2262-04-11 23:47:16.854775807')
>>> Timestamp(9223372036854775808)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 581, in pandas._libs.tslibs.timestamps.Timestamp.__new__
    ts = convert_to_tsobject(ts_input, tz, unit, 0, 0)
  File "pandas/_libs/tslibs/conversion.pyx", line 289, in pandas._libs.tslibs.conversion.convert_to_tsobject
    obj.value = ts
OverflowError: Python int too large to convert to C long

but an incorrect Timestamp is returned if an out-of-bounds datetime string is specified:

>>> Timestamp('2262-04-11 23:47:16.854775807')
Timestamp('2262-04-11 23:47:16.854775807')
>>> Timestamp('2262-04-11 23:47:16.854775808')
Timestamp('2262-04-11 23:47:16.854775')

Verified that this is the case for DatetimeIndex as well.

Expected Output

OutOfBoundsDatetime exception is raised

Output of pd.show_versions()

commit: b38dc41

python: 3.6.2.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0.dev0+169.gb38dc4105
pytest: 3.3.1
pip: 9.0.1
setuptools: 28.8.0
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.7.1
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.6.5
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: 1.5.1
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.1
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.15
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: 0.1.2
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeError ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions