Questions tagged [pandas-rolling]

questions related to the rolling method of pandas DataFrame and pandas Series objects

rolling method provides rolling window calculations.

Read more:

23 questions
4
votes
0 answers

Why does pd.df.rolling.aggregate takes so much longer when the input is a list?

I'm using python 3.8.3 and pandas 1.1.5. table_in.T is a 18 by 209997 pandas dataframe. window and periods are both set to 12. The following three lines of code respectively takes 9.52s, 6.27s and 5min53s to run. rolledsum2_in =…
Guolun Li
  • 77
  • 1
  • 6
4
votes
2 answers

ModuleNotFoundError: No module named 'pandas._libs.tslibs.frequencies'

I found several questions about the same issue here and here from pyfinance.ols import PandasRollingOLS I get the following error: Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyfinance/utils.py", line 78, in…
user12690225
3
votes
1 answer

Pandas: Get average of a dynamic number of rows

I have a dataframe with a timestamp column/index and I am calculating the moving average over the last 5 seconds. df['Mid-Price'].rolling(window=time_diff, min_periods=1, closed='both').mean() So far so good. Now I also need to calculate the moving…
3
votes
2 answers

pandas rolling apply function has slow performance

The source code in question is import numpy as np dd=lambda x: np.nanmax(1.0 - x / np.fmax.accumulate(x)) df.rolling(window=period, min_periods=1).apply(dd) It takes an extremely long time to execute the above 2 lines of code. It is with latest…
jagpreet
  • 53
  • 1
  • 10
3
votes
1 answer

Pandas rolling time window fails on count of string - why?

Trying to use pandas rolling with a timeindex and the count() method, getting an error, what am I missing here? here is an example: d = {'vv': {pd.Timestamp('2020-01-13 08:22:00', freq='T'): 'aa', pd.Timestamp('2020-01-13 08:23:00', freq='T'):…
Ezer K
  • 3,637
  • 3
  • 18
  • 34
2
votes
1 answer

How to get pandas to roll through entire series?

Pandas Rolling function Last elements when window_size == step_size I can't seem to get the last three element of an example 9 element series to be rolled on, when my window size and step size are both 3. Is the below an intended behaviour of…
semyd
  • 430
  • 4
  • 17
2
votes
2 answers

Pandas rolling window statistics calculation with input data with uneven timestamps

quick background: this can relate to pandas rolling, resample, asfreq, fillna this is based on processing timeseries data so I want to use pandas offset (e.g., '1T', '5min', etc.) as an input to methods. also, I'm applying forward looking window…
2
votes
2 answers

Compare two columns based on last N rows in a pandas DataFrame

I want to groupby "ts_code" and calculate percentage between one column max and min value from another column after max based on last N rows for each group. Specifically, df ts_code high low 0 A 20 10 1 A 30 5 2 A 40 20 3 A 50 …
Jack
  • 1,724
  • 4
  • 18
  • 33
2
votes
1 answer

Pandas groupby rolling drops index column

Not sure if I am doing something wrong (Pandas 1.2.5): ids = pd.DataFrame(data=range(10), columns=['Id']) dt = pd.DataFrame(pd.date_range('2021-01-01', '2021-01-10', freq='D'), columns=['Date']) df = ids.merge(dt, how='cross') df['Val'] =…
iggy
  • 662
  • 6
  • 14
1
vote
1 answer

Get rolling average without every timestamp

I have data about how many messages each account sends aggregated to an hourly level. For each row, I would like to add a column with the sum of the previous 7 days messages. I know I can groupby account and date and aggregate the number of messages…
ErrorJordan
  • 611
  • 5
  • 15
1
vote
0 answers

Is there any way to use Groupby and Rollong together?

I have the following dataframe with daily data: day value 2017-08-04 0.832 2017-08-05 0.892 2017-08-06 0.847 2017-08-07 0.808 2017-08-08 0.922 2017-08-09 0.894 2017-08-10 2.332 2017-08-11 0.886 2017-08-12 …
1
vote
1 answer

How to get previous 4 week sales at a level

I would like to find previous four week sales at a level in Python. Say for example ID Category Date Sales 1 AA 7/02/2022 1 1 AA 31/01/2022 3 1 AA 24/01/2022 5 1 AA 10/01/2022 7 1 AA 03/01/2022 9 2 BB 7/02/2022 2 2 …
Mounika G
  • 11
  • 3
1
vote
1 answer

Pandas Rolling Change Thresholds

Let's say I have two columns, 'a' and 'b', of time series data in a Pandas dataframe. I want to create a third column that indicates if the difference between column 'a' at the current time period and column 'b' at any of the next 5 time periods…
1
vote
1 answer

Count category duplicates within a time window in new column in python (similar to rolling with value_counts)

I have been trying to solve an exercise for some time and I haven’t been able to do it, I have a dataset containing a list of calls with the topic of the call (in this sample dataset I decided to use ice cream flavors as topics), In the call center…
1
vote
3 answers

Pandas groupby rolling for future values

I am trying to use a the pandas rolling function with window size 2 with groupby. This would be pretty standard other than that I also want the window to include the current value and the proceeding value. Specifically, given df =…
Ottpocket
  • 77
  • 12
1
2