Home > Net >  Substract one year to a Pandas Period Serie
Substract one year to a Pandas Period Serie

Time:10-26

I have a pandas series which is a Period object with frequency W-SUN

>>dft_history.date
dtype: period[W-SUN]

I wanna keep this frequency, but being able to filter values one year in the past from now on. To do so, I rest to this periods 1 year, but I get an error:

>>dft_history.date - pd.DateOffset(years=1)
IncompatibleFrequency: Input has different freq=<-1 * DateOffset: years=1> from PeriodArray(freq=W-SUN)

So I have tried to set other time frequencies to pd.DateOffset but still I could not achieve the result. Is there a way to do this keeping the W-SUN frequency?

CodePudding user response:

You can't subtract the Dateoffset from the Period also to compare two different periods they must have the same frequency. One way to approach this problem is to first make sure the frequency of the periods is same

lower = pd.Timestamp('now') - pd.DateOffset(years=1)
dft_history[dft_history['date'] >= lower.to_period('W-SUN')]
  • Related