Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 1000
Vadim Avatar asked Dec 18 '22 02:12

Vadim


1 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
like image 162
Bharath Avatar answered Dec 27 '22 01:12

Bharath