'time data 2018-01-01 00:00:00+00:00:00 doesn't match format specified

I am trying to create a DateTime series with the following date format: '2018-01-01 00:00:00+00:00:00'

time_steps = pd.to_datetime(f['forecast_time'][...].astype(str), format='%Y-%m-%d %H:%M:%S', errors='raise', utc = True)

And I am getting the following result:

time data 2018-01-01 00:00:00+00:00:00 doesn't match format specified

When errors= 'coerce' the df returns NaT, when errors = 'ignore' the dtype is Object instead of datetime64.

Please let me know what I am doing wrong



Solution 1:[1]

I think you can do this, as the UTC offset second should be zero (if the :00 is removed from the format, an error raise):

time_steps = pd.to_datetime(
    '2018-01-01 00:00:00+00:00:00', 
    format='%Y-%m-%d %H:%M:%S%z:00', 
    errors='raise', 
    utc = True
)

Here's a more accurate but dirty way

time_steps = pd.to_datetime(
    '2018-01-01 00:00:00+00:00:00'.replace('+', ' +').replace('-', ' -'), 
    format='%Y -%m -%d %H:%M:%S %z', 
    errors='raise', 
    utc = True
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zheng Bowen