Skip to content

BUG: 'now' and 'today' only parse in to_datetime with ISO8601 formats #50359

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

In [14]: to_datetime(['2000-01-01', 'now'], format='%Y-%m-%d', utc=True)
Out[14]: DatetimeIndex(['2000-01-01 00:00:00+00:00', '2022-12-20 12:04:20.955180+00:00'], dtype='datetime64[ns, UTC]', freq=None)

In [15]: to_datetime(['2000-01-01', 'now'], format='%Y-%d-%m', utc=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [15], line 1
----> 1 to_datetime(['2000-01-01', 'now'], format='%Y-%d-%m', utc=True)

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:150, in pandas._libs.tslibs.strptime.array_strptime()

ValueError: time data 'now' does not match format '%Y-%d-%m' (match)

Issue Description

the outputs should match

Expected Behavior

DatetimeIndex(['2000-01-01 00:00:00+00:00', '2022-12-20 12:04:20.955180+00:00'], dtype='datetime64[ns, UTC]', freq=None)

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions