You can use to_datetime
with parameter format
:
s = pd.Series(['01APR2017 6:59','01APR2017 6:59'])
print (s)
0 01APR2017 6:59
1 01APR2017 6:59
dtype: object
print (pd.to_datetime(s, format='%d%b%Y %H:%M'))
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00
dtype: datetime64[ns]
Another possible solution is use date_parser
in read_csv
:
import pandas as pd
from pandas.compat import StringIO
temp=u"""date
01APR2017 6:59
01APR2017 6:59"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
parser = lambda x: pd.datetime.strptime(x, '%d%b%Y %H:%M')
df = pd.read_csv(StringIO(temp), parse_dates=[0], date_parser=parser)
print (df)
date
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00
print (df.date.dtype)
datetime64[ns]
EDIT by comment:
If values cannot be parsed to datetime
, add parameter errors='coerce'
for convert to NaT
:
s = pd.Series(['01APR2017 6:59','01APR2017 6:59', 'a'])
print (s)
0 01APR2017 6:59
1 01APR2017 6:59
2 a
dtype: object
print (pd.to_datetime(s, format='%d%b%Y %H:%M', errors='coerce'))
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00
2 NaT
dtype: datetime64[ns]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…