- 生成时间序列
pd.data_range(start/end/periods(个数)/freq(间隔) 参数4选3
pd.date_range(start='20200101',periods=3,freq='D')
Out[3]:DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03'], dtype='datetime64[ns]', freq='D')
pd.date_range(start='20200101',periods=3,freq='3D')
DatetimeIndex(['2020-01-01', '2020-01-04', '2020-01-07'], dtype='datetime64[ns]', freq='3D')
pd.date_range(start='20200101',periods=3,freq='0.5D')
Out[10]:DatetimeIndex(['2020-01-01 00:00:00', '2020-01-01 12:00:00',
'2020-01-02 00:00:00'],
dtype='datetime64[ns]', freq='12H')
freq常用
符号 | D/B | W | M/Q/Y | BM/BQ/BY | MS/QS/YS | BMS/BQS/BYS | H | T | S |
---|---|---|---|---|---|---|---|---|---|
描述 | 日/工作日 | 周 | 月/季/年末日 | 月/季/年末工作日 | 月/季/年初日 | 月/季/年初工作日 | 小时 | 分钟 | 秒 |
- 常用时间函数
dayofweek函数结果里0对应周一;6对应周日
pd.to_datetime('20200629').dayofweek
0
resample函数
df_r = pd.DataFrame(np.random.randn(1000, 3),index=pd.date_range('1/1/2020', freq='10S', periods=1000),
columns=['A', 'B', 'C'])
df_r.head()
r = df_r.resample('0.5min')
r.sum()
窗口(window)函数:rolling
s = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2020', periods=1000))
s.head()
2020-01-01 0.802348
2020-01-02 -0.938157
2020-01-03 -1.700035
2020-01-04 1.549462
2020-01-05 0.367530
Freq: D, dtype: float64
s.rolling(window=3)
#1-3 2-4 3-5求和
s.rolling(window=3).sum()
2020-01-01 NaN
2020-01-02 NaN
2020-01-03 -0.653915
2020-01-04 -2.161374
2020-01-05 -2.481817
...
2022-09-22 4.451041
2022-09-23 3.216510
2022-09-24 -0.255216
2022-09-25 0.908862
2022-09-26 2.150317
Freq: D, Length: 1000, dtype: float64
#1-3 2-4 3-5求均值
s.rolling(window=3).mean()
2020-01-01 NaN
2020-01-02 NaN
2020-01-03 -0.217972
2020-01-04 -0.720458
2020-01-05 -0.827272
...
2022-09-22 1.483680
2022-09-23 1.072170
2022-09-24 -0.085072
2022-09-25 0.302954
2022-09-26 0.716772
Freq: D, Length: 1000, dtype: float64
未完待续