funk101
funk101

Reputation: 183

ValueError: time data '07-24-2018 16:40: 0' does not match format '%m-%d-%Y %H:%M:%S ' (match)

data2['DateTime'].head()
2    07-24-2018 16:40: 0
3    07-24-2018 16:45: 0
4    07-24-2018 16:50: 0
5    07-24-2018 16:55: 0
6    07-24-2018 18: 0: 0
Name: DateTime, dtype: object

Is this error due to the missing zero's in the dataframe? I should 'pad' the single zeros in dataframe?

Upvotes: 0

Views: 39

Answers (1)

cs95
cs95

Reputation: 402463

I would prefer you looked upstream to find out where and why those spaces are being introduced and fix them.

But in the meantime,

pd.to_datetime(df['DateTime'].str.replace(' 0', '0', regex=False))

Or, if you need to be a little more explicit,

pd.to_datetime(df.DateTime.str.replace(r'(?<=:)\s0', '0'))

2   2018-07-24 16:40:00
3   2018-07-24 16:45:00
4   2018-07-24 16:50:00
5   2018-07-24 16:55:00
6   2018-07-24 18:00:00
Name: DateTime, dtype: datetime64[ns]

Upvotes: 1

Related Questions