Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add n months to a pandas Period object?

This seems like it should be easy but I can find no answer when googling. Say I have some variable k that is of type pandas.Period, and whose value is:

Period('2018-11', 'M')

How do I add n months to this variable. For example if n is 3 i would want k to be

Period('2019-02', 'M')

I have tried the following:

k.month = k.month + 12

But this fails saying:

AttributeError: attribute 'month' of 'pandas._libs.tslibs.period._Period' objects is not writable
like image 338
sometimesiwritecode Avatar asked Mar 04 '23 16:03

sometimesiwritecode


1 Answers

Add a pd.offsets.MonthEnd object:

pd.Period('2018-11', 'M') + pd.offsets.MonthEnd(3)
like image 100
cs95 Avatar answered Mar 19 '23 02:03

cs95