Home > database >  How to convert string timeseries to datetime object in a dataframe while preserving the format of th
How to convert string timeseries to datetime object in a dataframe while preserving the format of th

Time:09-15

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)

CodePudding user response:

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')

CodePudding user response:

There is a space before your month value: ' 06-30-2022 19:41:15'. You need to strip the string before applying the to_datetime. Something like this:

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