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

Question:

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…

Asked By: Vadim

||

Answers:

You can groupby month and day, then shift i.e

df['Previous'] = df.groupby([df['Date'].dt.month,df['Date'].dt.day])['Value'].shift()

Sample Output :

        Date  Ticks  Value  Previous
0 2013-01-01      0     99       NaN
1 2013-01-02      1      0       NaN
2 2013-01-03      2      5       NaN
3 2014-01-01      3      0      99.0
4 2014-01-02      4      1       0.0
5 2014-01-03      2      5       5.0
7 2014-01-04      2      5       NaN
Answered By: Bharath

You can also use Pandas DateOffset. The help document is here.

For example, to get previous year, use

from pandas.tseries.offsets import DateOffset

df['Previous'] = df['Date'] - DateOffset(years=1)

If you want last month, you can try:

df['Previous'] = df['Date'] - DateOffset(months=1)
Answered By: Julia Zhao
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.