convert MMM yyyy into yyyy-mm in python3
so for a panda dateframe with dd mmm yyyy like
01 Feb 1985
It can be converted into yyyy-mm-dd with the following code
df[["date"]] = df[["date"]].apply(pd.to_datetime)
output:
1985-02-01
But if i have MMM yyyy like
Feb 1985
and I would like to output as
1985-02
How can I do that?
df[["date"]] = df[["date"]].apply(pd.to_datetime)
will output the date which i don't want
1985-02-01
Any thoughts?
Thanks for some comments. I just realized that there are NaN in the dataframe that I cannot drop. So I am trying to do a function to ingore NaN but the result is still like 1985-02-01. Any thoughts?
def date2int(df):
for index, row in df.iteritems():
try:
t1=row.strftime('%b-%Y')
return t1
except ValueError:
return None
date2int(df["date2"])