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

2

5

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

Wrennie answered 28/11, 2017 at 12:10 Comment(0)
S
11

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
Smew answered 28/11, 2017 at 12:17 Comment(2)
Usually pandas questions are answered quicker. Glad I got this one.Smew
What if we want last month instead? I would appreciate if you can explain the approach. At least, I didn't figure it out! @DarkRicercare
I
2

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)
Imine answered 1/11, 2022 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.