# rolling-computation

## Python pandas rolling sum positive number with duplicate timestamps

Python pandas rolling sum positive number with duplicate timestamps Question: have a dataframe with two columns like below. One column is datetime and another is purely numbers. I’d like to sum all positive numbers of last 5 minutes.Tried df[‘positive’] = df[‘number’].rolling(‘5T’).sum() but didn’t work. Somehow, getting a ValueError: window must be an integer 0 or …

## Python Polars Rolling Count

Python Polars Rolling Count Question: There are some known rolling functions in polars, namely rolling_mean(), rolling_apply() and rolling_max(). However, if I would like to get a count on the number of occurrence of a value in each window, how should that be done? Let’s say we now have a LazyFrame: df = pl.LazyFrame({"Date": ["2023-01-01", "2023-01-02", …

## How to add a new column to dataframe based on conditions on another column

How to add a new column to dataframe based on conditions on another column Question: I have the following example dataframe: d = {‘col1’: [4, 2, 8, 4, 3, 7, 6, 9, 3, 5]} df = pd.DataFrame(data=d) df col1 0 4 1 2 2 8 3 4 4 3 5 7 6 6 7 9 …

## Calculating a rolling weighted sum using numpy

Calculating a rolling weighted sum using numpy Question: I am curious to know if there are any more optimal ways to compute this "rolling weighted sum" (unsure what the actual terminology is, but I will provide an example to further clarify). I am asking this because I am certain that my current code snippet is …

## Pandas dataframe get expanding/rolling value counts

Pandas dataframe get expanding/rolling value counts Question: Is it possible to create value counts for a series in a cumulative/expanding fashion? E.g. for the following series pd.Series([‘a’, ‘a’, ‘b’, ‘c’, ‘c’]) I’d want [ ‘a’, ‘b’, ‘c’ 1, 0, 0 2, 0, 0 2, 1, 0 2, 1, 1 2, 1, 2] Asked By: Daniel …

## Groupby with forward looking rolling maximum

Groupby with forward looking rolling maximum Question: I have data with date, time, and values and want to calculate a forward looking rolling maximum for each date: Date Time Value Output 01/01/2022 01:00 1.3 1.4 01/01/2022 02:00 1.4 1.2 01/01/2022 03:00 0.9 1.2 01/01/2022 04:00 1.2 NaN 01/02/2022 01:00 5 4 01/02/2022 02:00 4 3 …

## Get max value in previous rows for matching rows

Get max value in previous rows for matching rows Question: Say I have a dataframe that records temperature measurements for various sensors: import pandas as pd df = pd.DataFrame({‘sensor’: [‘A’, ‘C’, ‘A’, ‘C’, ‘B’, ‘B’, ‘C’, ‘A’, ‘A’, ‘A’], ‘temperature’: [4.8, 12.5, 25.1, 16.9, 20.4, 15.7, 7.7, 5.5, 27.4, 17.7]}) I would like to add …

## Python: resample on a rolling basis

Python: resample on a rolling basis Question: I have a DataFrame as follows: data = [[99330,12,122], [1123,1230,1287], [123,101,812739], [1143,12301230,252], [234,342,4546], [2445,3453,3457], [7897,8657,5675], [46,5675,453], [76,484,3735], [363,93,4568], [385,568,367], [458,846,4847], [574,45747,658468], [57457,46534,4675]] df1 = pd.DataFrame(data, index=[‘2022-01-01’, ‘2022-01-02’, ‘2022-01-03’, ‘2022-01-04’, ‘2022-01-05’, ‘2022-01-06’, ‘2022-01-07’, ‘2022-01-08’, ‘2022-01-09’, ‘2022-01-10’, ‘2022-01-11’, ‘2022-01-12’, ‘2022-01-13’, ‘2022-01-14’], columns=[‘col_A’, ‘col_B’, ‘col_C’]) df1.index = pd.to_datetime(df1.index) df1.resample(‘1D’).last().rolling(7).last() The last …