Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating daily aggregates on pandas with custom function

I'm new to Pandas, and as an exercise, I'm moving some old code / solutions to learn from it. In this case, I'm trying to calculate a synthetic index for product prices, which before it was calculated in SQL.

This is the data I've in the dataframe:

    id  weight      date        price
0   1   0.002796    2005-11-15  0.998298
1   1   0.002796    2005-11-16  1.014242
2   1   0.002796    2005-11-17  1.016452
3   1   0.002796    2005-11-18  1.026396
4   1   0.002796    2005-11-19  1.026047
5   1   0.002796    2005-11-20  1.024285
6   1   0.002796    2005-11-21  1.018764
7   1   0.002796    2005-11-22  1.033175
8   1   0.002796    2005-11-23  1.058509
9   1   0.002796    2005-11-24  1.061231
10  1   0.002796    2005-11-25  1.058137
11  1   0.002796    2005-11-26  0.999380
12  1   0.002796    2005-11-27  0.990504
13  1   0.002796    2005-11-28  0.993764
14  1   0.002796    2005-11-29  0.978754
15  1   0.002796    2005-11-30  0.992070
...     ...     ...     ...     ...
4085    1   0.002796    2017-01-21  0.857420
4086    1   0.002796    2017-01-22  0.848195
4087    1   0.002796    2017-01-23  0.791784
4088    1   0.002796    2017-01-24  0.846603
4089    1   0.002796    2017-01-25  0.878104
4090    1   0.002796    2017-01-26  0.806651
4091    1   0.002796    2017-01-27  0.849316
4092    1   0.002796    2017-01-28  0.826550
4093    1   0.002796    2017-01-29  0.848651
4094    1   0.002796    2017-01-30  0.829643
4095    1   0.002796    2017-01-31  0.837094
4096    1   0.002796    2017-02-01  0.846572
4097    1   0.002796    2017-02-02  0.800163
4098    1   0.002796    2017-02-03  0.820356
4099    1   0.002796    2017-02-04  0.818924
4100    1   0.002796    2017-02-05  0.822157
4101    1   0.002796    2017-02-06  0.787123
4102    1   0.002796    2017-02-07  0.796264
4103    1   0.002796    2017-02-08  0.797241
4104    1   0.002796    2017-02-09  0.818499
4105    1   0.002796    2017-02-10  0.810928

The synthetic index is calculated with daily returns, being the return for the day:

Rt = ( Price_day / Price_day_before) - 1

I've been reading about pandas, time series, etc, but I'm struggling to understand the specific operation to perform here; is this a rolling? How do I get the data for a given date and the date before??

like image 880
Alberto Avatar asked Mar 09 '23 21:03

Alberto


1 Answers

the code is:

df['return'] = df['price']/df['price'].shift(1) - 1

the df.shift(1) function in python is used as LEAD() or LEG() function in SQL.

like image 120
heyu91 Avatar answered Apr 09 '23 04:04

heyu91