Reputation: 184
I have a pandas DataFrame called df that has the following data:
Index SourceDate
0 AUG_2013
1 SEP_2013
2 JAN_2012
I need to add an additional column that turns each of these dates into the following ConvertedDate Column. This column will have the date in YYYY-MM-DD format with the day always being 01.
Index SourceDate ConvertedDate
0 AUG_2013 2013-08-01
1 SEP_2013 2013-09-01
2 JAN_2012 2012-01-01
I attempted doing this with:
df['ConvertedDate'] = time.strptime(str.replace(str.rsplit(df.SourceDate,'_',1)[0],'_','-01-'),'%b-%d-%Y')
Unfortunately this does not work since df.SourceDate is a Series, and string functions won't work on a Series.
Upvotes: 2
Views: 1240
Reputation: 394389
Use to_datetime
and pass a format string:
In [64]:
df['ConvertedDate'] =pd.to_datetime(df['SourceDate'], format='%b_%Y')
df
Out[64]:
Index SourceDate ConvertedDate
0 0 AUG_2013 2013-08-01
1 1 SEP_2013 2013-09-01
2 2 JAN_2012 2012-01-01
The python datetime format string specifiers can be found here
Upvotes: 2