pandas记录之时间序列

  1. 生成时间序列
    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/BWM/Q/YBM/BQ/BYMS/QS/YSBMS/BQS/BYSHTS
描述日/工作日月/季/年末日月/季/年末工作日月/季/年初日月/季/年初工作日小时分钟
  1. 常用时间函数
    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

未完待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值