Search code examples
pythonpandasoffsetmoving-averagedatetimeoffset

How do I calculate week over week changes in Pandas?


I have the following df of values for various slices across time:

    date        A   B   C
0   2016-01-01  5   7   2
1   2016-01-02  6   12  15
...
2   2016-01-08  9   5   16
...
3   2016-12-24  5   11  13
4   2016-12-31  3   52  22

I would like to create a new dataframe that calculates the w-w change in each slice, by date. For example, I want the new table to be blank for all slices from jan 1 - jan 7. I want the value of jan 8 to be the jan 8 value for the given slice minus the value of the jan 1 value of that slice. I then want the value of jan 9 to be the jan 9 value for the given slice minus the value of the jan 2 slice. So and so forth, all the way down.

The example table would look like this:

    date        A   B   C
0   2016-01-01  0   0   0
1   2016-01-02  0   0   0
...
2   2016-01-08  4   -2  14
...
3   2016-12-24  4   12  2
4   2016-12-31  -2  41  9

You may assume the offset is ALWAYS 7. In other words, there are no missing dates.


Solution

  • If we know offset is always 7 then use shift(), here is a quick example showing how it works :

    df = pandas.DataFrame({'x': range(30)})
    df.shift(7)
           x
    0    NaN
    1    NaN
    2    NaN
    3    NaN
    4    NaN
    5    NaN
    6    NaN
    7    0.0
    8    1.0
    9    2.0
    10   3.0
    11   4.0
    12   5.0
    ...
    

    So with this you can do :

    df - df.shift(7)
          x
    0   NaN
    1   NaN
    2   NaN
    3   NaN
    4   NaN
    5   NaN
    6   NaN
    7   7.0
    8   7.0  
    ...
    

    In your case, don't forget to set_index('date') before.