Dervin Thunk
Dervin Thunk

Reputation: 20119

How do I calculate the difference between a day and the corresponding day a week before in pandas?

I hava pandas DataFrame like this, where date is the index:

| date       |   cases |
|:-----------|--------:|
| 2020-03-30 |       5 |
| 2020-04-01 |       9 |
| 2020-04-03 |      10 |
| 2020-04-06 |      13 |
| 2020-04-08 |      16 |

I would like to calculate the difference between some date, and the date a week before. So in the case above it would be 8 for the 2020-04-06 (13 for the 2020-04-06 - 2020-03-30), 7 for 2020-04-08 (- 2020-04-01), and so on.

I've tried with df.diff and df.shift(freq=7) but sometimes the offset days are not a constant.

Upvotes: 1

Views: 80

Answers (2)

BENY
BENY

Reputation: 323226

Let us try shift

s = df.set_index('date').cases
df['diff' ] = df.date.map(s - s.shift(freq='7 D'))
df
Out[219]: 
        date  cases  diff
0 2020-03-30      5   NaN
1 2020-04-01      9   NaN
2 2020-04-03     10   NaN
3 2020-04-06     13   8.0
4 2020-04-08     16   7.0

Upvotes: 2

anky
anky

Reputation: 75080

Try subtracting a week from the date and then map the cases based on date , then subtract:

df['cases']-(df['date'] - pd.Timedelta(weeks=1)).map(df.set_index('date')['cases'])

0    NaN
1    NaN
2    NaN
3    8.0
4    7.0
dtype: float64

df['Difference'] = (df['cases']-(df['date'] - pd.Timedelta(weeks=1))
                .map(df.set_index('date')['cases']))
print(df)

        date  cases  Difference
0 2020-03-30      5         NaN
1 2020-04-01      9         NaN
2 2020-04-03     10         NaN
3 2020-04-06     13         8.0
4 2020-04-08     16         7.0

Upvotes: 2

Related Questions