I have a series with some datetimes (like strings) and some zeros like "nan":
import pandas as pd, numpy as np, datetime as dt
df = pd.DataFrame({'Date':['2014-10-20 10:44:31', '2014-10-23 09:33:46', 'nan', '2014-10-01 09:38:45']})
I am trying to convert them to datetime:
df['Date'] = df['Date'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
but I get the error:
time data 'nan' does not match format '%Y-%m-%d %H:%M:%S'
So, I'm trying to turn them into real zeros:
df.ix[df['Date'] == 'nan', 'Date'] = np.NaN
and repeat:
df['Date'] = df['Date'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
but then I get the error:
should be a string, not a float
What is the fastest way to solve this problem?
source
share