1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] time data jan 5 2017 doesn't match format in jupyter notebook

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 20:32.

  1. Stack

    Stack Membro Participativo

    import pandas as pd
    dates=['2017-01-05','Jan 5,2017','20170105']
    pd.to_datetime(dates)


    I am expecting the date would be 2017-01-5(YY/mm/dd).But I found Value error:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\dev\Python312\Lib\site-packages\pandas\core\tools\datetimes.py", line 1099, in to_datetime
    result = convert_listlike(argc, format)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\dev\Python312\Lib\site-packages\pandas\core\tools\datetimes.py", line 433, in _convert_listlike_datetimes
    return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\dev\Python312\Lib\site-packages\pandas\core\tools\datetimes.py", line 467, in _array_strptime_with_fallback
    result, tz_out = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "strptime.pyx", line 501, in pandas._libs.tslibs.strptime.array_strptime
    File "strptime.pyx", line 451, in pandas._libs.tslibs.strptime.array_strptime
    File "strptime.pyx", line 583, in pandas._libs.tslibs.strptime._parse_with_format
    ValueError: time data "Jan 5,2017" doesn't match format "%Y-%m-%d", at position 1. 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.


    Please give me a solution. Please tell me the right way to do it. I want the date would be same.

    Continue reading...

Compartilhe esta Página