Skip to content
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

BUG: scalar out of bounds input does not raise for to_datetime #60677

Open
2 of 3 tasks
kmuehlbauer opened this issue Jan 8, 2025 · 2 comments
Open
2 of 3 tasks

BUG: scalar out of bounds input does not raise for to_datetime #60677

kmuehlbauer opened this issue Jan 8, 2025 · 2 comments
Labels
Bug datetime.date stdlib datetime.date support Error Reporting Incorrect or improved errors from pandas

Comments

@kmuehlbauer
Copy link
Contributor

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd
uint64_max = np.iinfo("uint64").max

try:
    dtime = pd.to_datetime(uint64_max, unit="ns")
    print("Wrong:", dtime)
    dtime = pd.to_datetime([uint64_max], unit="ns")
except Exception as err:
    print(err)

Wrong: 1969-12-31 23:59:59.999999999
cannot convert input 18446744073709551615 with the unit 'ns', at position 0

Issue Description

Trying to convert out of bounds input does not raise for scalar input.

Expected Behavior

Should raise.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.4
python-bits : 64
OS : Linux
OS-release : 5.14.21-150500.55.83-default
Version : #1 SMP PREEMPT_DYNAMIC Wed Oct 2 08:09:07 UTC 2024 (0d53847)
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8

pandas : 2.2.3
numpy : 2.2.1
pytz : 2024.1
dateutil : 2.9.0
pip : 24.0
Cython : None
sphinx : 8.1.0
IPython : 8.25.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : 1.4.0
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.6.1
html5lib : None
hypothesis : 6.114.1
gcsfs : None
jinja2 : 3.1.4
lxml.etree : 5.2.2
matplotlib : 3.10.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 17.0.0
pyreadstat : None
pytest : 8.2.2
python-calamine : None
pyxlsb : None
s3fs : 2024.6.1
scipy : 1.14.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : 2025.1.1.dev3+g251329e3
xlrd : None
xlsxwriter : None
zstandard : 0.22.0
tzdata : 2024.1
qtpy : None
pyqt5 : None

@kmuehlbauer kmuehlbauer added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2025
@rhshadrach
Copy link
Member

Thanks for the report! Confirmed on main, further investigations and PRs to fix are welcome!

@rhshadrach rhshadrach added datetime.date stdlib datetime.date support Error Reporting Incorrect or improved errors from pandas and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2025
@fynnsu
Copy link

fynnsu commented Jan 9, 2025

I've narrowed down the source of the issue.

First, the reason these two cases behave different is that:

  • the scalar uint64_max gets converted to np.array([uint64_max]) (which infers dtype='uint64')
  • the list [uint64_max] get converted to np.array([uint64_max], dtype='O')

You can confirm this by passing either of the converted forms directly into pd.to_datetime

Then there is special handling for int/uint/float np.arrays in pandas/core/tools/datetimes.py _to_datetime_with_unit():

        if arg.dtype.kind in "iu":
            # Note we can't do "f" here because that could induce unwanted
            #  rounding GH#14156, GH#20445
            arr = arg.astype(f"datetime64[{unit}]", copy=False) # <---- LINE 1
            try:
                arr = astype_overflowsafe(arr, np.dtype("M8[ns]"), copy=False) # <---- LINE 2 
            except OutOfBoundsDatetime:
                if errors == "raise":
                    raise
                arg = arg.astype(object)
                return _to_datetime_with_unit(arg, unit, name, utc, errors)
            tz_parsed = None

Line 2 correctly handles overflow between different datetime formats (e.g. ns to s) but at this stage Line 1 has already converted from uint64 to datetime64[ns] with uncaught overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug datetime.date stdlib datetime.date support Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

No branches or pull requests

3 participants