(2026.04.04 Sat @崇左)
滑動窗口計(jì)算 pandas.rolling
pandas.rolling方法用于計(jì)算DataFrame中Series或DataFrame對象的滑動窗口的各項(xiàng)指標(biāo),聚合和變換函數(shù)包括min, max, sum, mean,對時(shí)間序列分析和平滑噪聲數(shù)據(jù)尤其有效。
案例:
import pandas as pd
import numpy as np
df = pd.DataFrame({'value': [10, 20, 30, np.nan, 50, 60]})
# 計(jì)算窗口尺寸為3的均值
rolling_mean = df['value'].rolling(window=3, min_periods=1).mean()
print(rolling_mean)
0 10.000000
1 15.000000
2 20.000000
3 25.000000
4 40.000000
5 55.000000
Name: value, dtype: float64
可自定義參數(shù):
-
window: 窗口尺寸 -
min_periods: 用于計(jì)算值的最小觀察區(qū)間,默認(rèn)值和window值相同 -
center:Align labels at the center of the window if True. -
win_type:應(yīng)用加權(quán)窗,e.g.,triang,guassian,默認(rèn)均勻加權(quán) -
on:用于基于時(shí)間的窗的字段 -
step(pandas 1.5+):Evaluate every nth step in the rolling window.
返回的結(jié)果和原Series的尺寸相同,無法賦值的部分用NaN填充。
pandas.resample
pandas.resample/df.resample方法用于對時(shí)間序列進(jìn)行重采樣,允許用戶將一個(gè)時(shí)間序列從一種頻率轉(zhuǎn)換為另一種頻率,并可對常規(guī)時(shí)間序列進(jìn)行聚合或計(jì)算統(tǒng)計(jì)值。
案例:
import pandas as pd
# 創(chuàng)建
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
data = np.random.rand(len(dates))
df = pd.DataFrame(data, index=dates, columns=['Random Data'])
# 重采樣改為月度數(shù)據(jù)
monthly_resampled_data = df.resample('M').mean()
print(monthly_resampled_data.head())
參數(shù):
-
rule: The offset string or object representing target conversion. -
axis: Which axis to use for up- or down-sampling. -
closed: Which side of bin interval is closed. -
label: Which bin edge label to label bucket with. -
convention: For PeriodIndex only, controls whether to use the start or end of rule. -
on: For a DataFrame, column to use instead of index for resampling. Column must be datetime-like. -
level: For a MultiIndex, level (name or number) to use for resampling. Level must be datetime-like.