Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format pandas datatime object to show fiscal years from Feb to Feb and be formatted as %Y?

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.

like image 657
JAB Avatar asked Mar 20 '23 08:03

JAB


2 Answers

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.

like image 189
Featherlegs Avatar answered Mar 22 '23 23:03

Featherlegs


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')
like image 38
JAB Avatar answered Mar 22 '23 21:03

JAB