Format pandas datatime object to show fiscal years from Feb to Feb and be formatted as %Y?
Question:
I have a series of timestamps like so,
3 2013-08-23 00:00:00
4 2008-09-21 00:00:00
5 2012-03-17 00:00:00
6 2011-12-31 00:00:00
7 2011-11-16 00:00:00
8 2008-01-23 00:00:00
9 2010-06-13 00:00:00
I want to convert them to Fiscal Years in the format 2010, 2011 etc. The fiscal Year runs FEB-JAN.
I am confused- do I need to use offset? How do I change the represention from a timestamp to just the fiscal year portion?
Thank you.
Answers:
I found an answer. Unlikely to be the best one or if so that’s serendipity for you.
df['DATE']=pd.to_datetime(df.DATE).apply(pd.Period, freq='A-FEB')
df['DATE'] = pd.to_datetime(df.DATE).dt.to_period('A-JAN')
Should be faster and what you want, as A-JAN
is a fiscal year that ends in January, as opposed to A-FEB
, which ends in February. See the description of the frequency strings for more information.
I have a series of timestamps like so,
3 2013-08-23 00:00:00
4 2008-09-21 00:00:00
5 2012-03-17 00:00:00
6 2011-12-31 00:00:00
7 2011-11-16 00:00:00
8 2008-01-23 00:00:00
9 2010-06-13 00:00:00
I want to convert them to Fiscal Years in the format 2010, 2011 etc. The fiscal Year runs FEB-JAN.
I am confused- do I need to use offset? How do I change the represention from a timestamp to just the fiscal year portion?
Thank you.
I found an answer. Unlikely to be the best one or if so that’s serendipity for you.
df['DATE']=pd.to_datetime(df.DATE).apply(pd.Period, freq='A-FEB')
df['DATE'] = pd.to_datetime(df.DATE).dt.to_period('A-JAN')
Should be faster and what you want, as A-JAN
is a fiscal year that ends in January, as opposed to A-FEB
, which ends in February. See the description of the frequency strings for more information.