# -*- coding: utf-8 -*-
"""
Created on Sun Mar 27 22:56:24 2022
@author: wuxian
"""
import pandas as pd
import datetime
import numpy as np
(datetime.date.today()).strftime('%Y-%m-%d')
#H:小时 D:天 M:月
#times #2016 Jul 7/1/2016 2016-07-01 2016/07/01
pd.date_range('2022-01-14',periods=10,freq='3d')
#np.random.randn(20)标准正太分布
df0 = pd.Series(np.random.randn(20),index=pd.date_range('2022-01-14',periods=20,freq='3d'))
#重采样 升采样 降采样
df0.resample('M').sum()
df0.resample('3D').sum()
#当我们对这个数据进行聚合的的时候,每个组只有一个值,以及gap(间隔)之间的缺失值。在不使用任何聚合函数的情况下,我们使用asfreq方法将其转换为高频度:
df0.resample('D').asfreq()
df0.resample('D').bfill(1)
df0.resample('D').ffill(1)
df0.resample('D').interpolate('linear') #线性拟合
#滑动窗口
df1=pd.Series(np.random.randn(600),index=pd.date_range('2020-01-14',periods=600,freq='d'))
r=df1.rolling(window=10)
r.mean()
import matplotlib.pyplot as plt
plt.figure(figsize=(15,5))
df1.plot(style='r--')
df1.rolling(window=10).mean().plot(style='b')
plt.show()
时间序列处理
于 2022-04-28 18:21:52 首次发布