[Python] pandas conert date failed - pd.to_datetime(df['xxx'], format='%Y-%m-%d').dt.date

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing one little problem. I am storing some date time data and the data is

    #secCode,secName,announcementTitle,announcementId,announcementTime

    003816,xxx name,2024report,1222913141,1743004800000


    the date time column is clearly string - 1743004800000, so when I try to convert it

    df['announcementTime'] = pd.to_datetime(df['announcementTime'], format='%Y-%m-%d').dt.date


    I got this error

    File "/root/miniconda3/lib/python3.12/site-packages/pandas/core/tools/datetimes.py", line 1072, in to_datetime
    values = convert_listlike(arg._values, format)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/root/miniconda3/lib/python3.12/site-packages/pandas/core/tools/datetimes.py", line 435, in _convert_listlike_datetimes
    return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ValueError: time data "1743004800000" doesn't match format "%Y-%m-%d", at position 0. You might want to try:
    - passing `format` if your strings have a consistent format;
    - passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
    - passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.
    df['announcementTime'] = pd.to_datetime(df['announcementTime'], format='%Y-%m-%d')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/root/miniconda3/lib/python3.12/site-packages/pandas/core/tools/datetimes.py", line 1072, in to_datetime

    Continue reading...

Compartilhe esta Página