pandas rolling window mean in the future

2024/9/20 9:34:19

I would like to use the pandas.DataFrame.rolling method on a data frame with datetime to aggregate future values. It looks it can be done only in the past, is it correct?

Answer

IIUC, you can use shift to move you calculation back in time.

df = pd.DataFrame({'Data':np.arange(0,11,1)},index=pd.date_range('2018-07-23',periods=11))df['rolling'] = df.rolling('2D').mean().shift(-1)print(df)

Output:

            Data  rolling
2018-07-23     0      0.5
2018-07-24     1      1.5
2018-07-25     2      2.5
2018-07-26     3      3.5
2018-07-27     4      4.5
2018-07-28     5      5.5
2018-07-29     6      6.5
2018-07-30     7      7.5
2018-07-31     8      8.5
2018-08-01     9      9.5
2018-08-02    10      NaN
https://en.xdnf.cn/q/72359.html

Related Q&A

When should I use type checking (if ever) in Python?

Im starting to learn Python and as a primarily Java developer the biggest issue I am having is understanding when and when not to use type checking. Most people seem to be saying that Python code shoul…

Kartograph python script generates SVG with incorrect lat/long coords

I have modified this question to reflect some progress on discovering the problem. I am using the following python code to generate an SVG of the continental USA. The shapefile is irrelevant, as the …

Python multiprocessing blocks indefinately in waiter.acquire()

Can someone explain why this code blocks and cannot complete?Ive followed a couple of examples for multiprocessing and Ive writting some very similar code that does not get blocked. But, obviously, I…

What is the best way to control Twisteds reactor so that it is nonblocking?

Instead of running reactor.run(), Id like to call something else (I dunno, like reactor.runOnce() or something) occasionally while maintaining my own main loop. Is there a best-practice for this with …

Accessing the content of a variable array with ctypes

I use ctypes to access a file reading C function in python. As the read data is huge and unknown in size I use **float in C . int read_file(const char *file,int *n_,int *m_,float **data_) {...}The func…

What is the stack in Python?

What do we call "stack" in Python? Is it the C stack of CPython? I read that Python stackframes are allocated in a heap. But I thought the goal of a stack was... to stack stackframes. What …

Pandas: Resample dataframe column, get discrete feature that corresponds to max value

Sample data:import pandas as pd import numpy as np import datetimedata = {value: [1,2,4,3], names: [joe, bob, joe, bob]} start, end = datetime.datetime(2015, 1, 1), datetime.datetime(2015, 1, 4) test =…

How to filter string in multiple conditions python pandas

I have following dataframeimport pandas as pd data=[5Star,FiveStar,five star,fiv estar] data = pd.DataFrame(data,columns=["columnName"])When I try to filter with one condition it works fine.d…

Is there a way to use a dataclass, with fields with defaults, with __slots__

I would like to put __slots__ on a dataclass with fields with defaults. When I try do that, I get this error: >>> @dataclass ... class C: ... __slots__ = (x, y, ) ... x: int ... y:…

Read remote file using python subprocess and ssh?

How can I read data from a big remote file using subprocess and ssh?