Reputation: 19375
Consider this simple example
df = pd.DataFrame({'mydate' : ['1985m3','1985m4','1985m5']})
df
Out[18]:
mydate
0 1985m3
1 1985m4
2 1985m5
How can I convert these monthly periods into a proper datetime
(artificially using the first day of the month, such as '1985-03-01'
for the first row)?
I can of course strip the month and do it the hard way but is there a better pandonic way?
Thanks!
Upvotes: 2
Views: 241
Reputation: 153460
Try using pandas.to_datetime
with Python time directives where '%Y' for year, 'm' hard code for the letter m, and '%m' for month:
pd.to_datetime(df.mydate, format='%Ym%m')
Output:
0 1985-03-01
1 1985-04-01
2 1985-05-01
Name: mydate, dtype: datetime64[ns]
Upvotes: 4