Python with pandas calculate data between quarterly of hour

Viewed 55

In my django project i create a queryset opbject with this structure:

|id|var_val|read_date                    |
|1 |134.54 |2021-06-15 16:09:03.367037+00|
|2 |12.34  |2021-06-15 16:03:03.987747+00|
...

well, i would using panda for example (of if is better numpy) aggregate data for every hour-quarterly and do some calculation with var_val values, for example the sum of var_val for data between every quarterly of hour ...

What is the best way for achieve this in python?

So many thanks in advance

1 Answers

I'm not sure if I fully understand your problem, but maybe I can answer a component of it.

Assuming read_date has been converted to a timestamp (e.g. using pd.to_datetime) and that your var_val is sorted, then you could compute the difference between first/last read values by:

rs = df.resample('15min', on='read_date').agg(['first', 'last'])
rs['diff'] = rs['first'] - rs['last']

You can use this pattern with other built-in or custom aggregation functions.

Related