Skip to content

ERR: "day out of range" doesn't show position of error #50462

Closed
@MarcoGorelli

Description

@MarcoGorelli

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

to_datetime(['2015-29-02'], format='%Y-%d-%m')

Issue Description

On both 1.5.2 and on main, the above shows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [3], line 1
----> 1 to_datetime(['2015-29-02'], format='%Y-%d-%m')

File ~/tmp/.venv/lib/python3.8/site-packages/pandas/core/tools/datetimes.py:1100, in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
   1098         result = _convert_and_box_cache(argc, cache_array)
   1099     else:
-> 1100         result = convert_listlike(argc, format)
   1101 else:
   1102     result = convert_listlike(np.array([arg]), format)[0]

File ~/tmp/.venv/lib/python3.8/site-packages/pandas/core/tools/datetimes.py:430, in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    427         format = None
    429 if format is not None:
--> 430     res = _to_datetime_with_format(
    431         arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
    432     )
    433     if res is not None:
    434         return res

File ~/tmp/.venv/lib/python3.8/site-packages/pandas/core/tools/datetimes.py:538, in _to_datetime_with_format(arg, orig_arg, name, tz, fmt, exact, errors, infer_datetime_format)
    535         return _box_as_indexlike(result, utc=utc, name=name)
    537 # fallback
--> 538 res = _array_strptime_with_fallback(
    539     arg, name, tz, fmt, exact, errors, infer_datetime_format
    540 )
    541 return res

File ~/tmp/.venv/lib/python3.8/site-packages/pandas/core/tools/datetimes.py:473, in _array_strptime_with_fallback(arg, name, tz, fmt, exact, errors, infer_datetime_format)
    470 utc = tz == "utc"
    472 try:
--> 473     result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
    474 except OutOfBoundsDatetime:
    475     if errors == "raise":

File ~/tmp/.venv/lib/python3.8/site-packages/pandas/_libs/tslibs/strptime.pyx:324, in pandas._libs.tslibs.strptime.array_strptime()

File ~/tmp/.venv/lib/python3.8/site-packages/pandas/_libs/tslibs/strptime.pyx:310, in pandas._libs.tslibs.strptime.array_strptime()

ValueError: day is out of range for month

Expected Behavior

ValueError: as position 0, day is out of range for month

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.6.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions