Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert string timeseries to datetime object in a dataframe while preserving the format of the original datetime?

I have a pandas dataframe whose first column is string representation of timeseries. I would like to convert this column into datetime object using the following line of code:

df['Timestamp'] = pd.to_datetime(
        df['Timestamp'], format='%m-%d-%Y %H:%M:%S').dt.strftime('%Y-%m-%d %H:%M:%S')

I want to preserve the format in the datetime object to be month first and year last but I am receiving an error message:

df['Timestamp'] = pd.to_datetime(
  File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 1051, in to_datetime
    values = convert_listlike(arg._values, format)
  File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 394, in _convert_listlike_datetimes
    res = _to_datetime_with_format(
  File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 514, in _to_datetime_with_format
    raise err
  File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 501, in _to_datetime_with_format
    res = _array_strptime_with_fallback(
  File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 437, in _array_strptime_with_fallback
    result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
  File "pandas\_libs\tslibs\strptime.pyx", line 150, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data ' 06-30-2022 19:41:15' does not match format '%m-%d-%Y %H:%M:%S' (match)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The format inside pd.to_datetime is forcing pd.to_datetime to seek only datetimes with this format. Remove it and just apply dt.strftime:

df['Timestamp'] = pd.to_datetime(df['Timestamp']).dt.strftime('%Y-%m-%d %H:%M:%S')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading