Pandas: get a value from previous year for the same day of the year

Viewed 6321

I'm looking for a way how to get a value from the previous year for the same day.

F.e. we have a value for 2014-01-01 and I want to create a new column with the value for this day but from one year ago.

a sample of the table, and I want to get the Previos_Year column.

   Date   Ticks Previos_Year 
2013-01-01  0        NaN
2013-01-02  1        NaN
2013-01-03  2        NaN
....
2014-01-01  3         0
2014-01-02  4         1

What have I tried so far:

I created a new column day of the year,

df['Day_in_Year'] = df.Date.dt.dayofyear

but I could not figure out how to use it for my task.

Also, I tried the shift fucntion:

df['Ticks'].shift(365)

and it works, until a leap year...

1 Answers
Related