1

I am working with some historical data on fiscal transfers in Canada. The downloaded data is in the format of fiscal year i.e.

Year     Quebec  Alberta
1980-1981   2000    4000
1981-1982   3000    6000

I am using the pandas library. However, when I try to make any visualizations using either matplot or sns, it generates an error either not recognizing 'Year' as a numerical value or ('DataFrame' object has no attribute 'Year'). However, when I change the values in the csv to a single year i.e.

Year     Quebec  Alberta
1980    2000    4000
1981    3000    6000

it works perfectly fine. Is there a way for Python to treat fiscal year values like 1980-1981 the same as normal year. Any advice would be much appreciated.

1 Answer 1

0

You can use 2years periods, but if print DataFrame columns cannot see end year:

print (df)
   Year  Quebec  Alberta
0  1980    2000     4000
1  1981    3000     6000
    
df['Year'] =  df['Year'].apply(lambda x: pd.Period(x, freq='2A-DEC'))

print (df['Year'])
0    1980
1    1981
Name: Year, dtype: period[2A-DEC]

print (df['Year'].dt.to_timestamp('A', how='s'))
0   1980-12-31
1   1981-12-31
Name: Year, dtype: datetime64[ns]

print (df['Year'].dt.to_timestamp('A', how='e'))
0   1981-12-31 23:59:59.999999999
1   1982-12-31 23:59:59.999999999
Name: Year, dtype: datetime64[ns]

But I think most easier is create 2 columns for start and end year:

print (df)
        Year  Quebec  Alberta
0  1980-1981    2000     4000
1  1981-1982    3000     6000

df[['StartYear','EndYear']] = df['Year'].str.split('-', expand=True).astype(int)
print (df)
        Year  Quebec  Alberta  StartYear  EndYear
0  1980-1981    2000     4000       1980     1981
1  1981-1982    3000     6000       1981     1982
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.