Pandas 时间序列处理
目录
Pandas 时间序列处理1 Python 的日期和时间处理1.1 常用模块
1.2 字符串和 datetime 转换datetime -> str
dt_obj = datetime(2019,8,8) str_obj = str(dt_obj) print(type(str_obj)) print(str_obj)
str_obj2 = dt_obj.strftime('%d/%m/%Y') print(str_obj2)
str -> datetime
dt_str = '2019-08-8' dt_obj2 = datetime.strptime(dt_str,'%Y-%m-%d') print(type(dt_obj2)) print(dt_obj2)
from dateutil.parser import parse dt_str2 = '8-08-2019' dt_obj3 = parse(dt_str2) print(type(dt_obj3)) print(dt_obj3)
2 Pandas 的时间处理及操作2.1 创建与基础操作基本类型,以时间戳为索引的 Series->Datetimelndex 指定 index 为 datetime 的 listfrom datetime import datetime import pandas as pd import numpy as np # 指定index为datetime的list date_list = [datetime(2017,2,18),datetime(2017,19),25),26),3,4),5)] time_s = pd.Series(np.random.randn(6),index=date_list) print(time_s) print(type(time_s.index))
索引
2017-02-18 -0.230989 2017-02-19 -0.398082 2017-02-25 -0.309926 2017-02-26 -0.179672 dtype: float64
2017-02-26 -0.179672 2017-03-04 0.942698 2017-03-05 1.053092 dtype: float64 过滤
2017-02-25 -0.309926 2017-02-26 -0.179672 2017-03-04 0.942698 2017-03-05 1.053092 dtype: float64
2017-02-18 -0.230989 2017-02-19 -0.398082 2017-02-25 -0.309926 dtype: float64 pd.date_range()功能:生成日期范围 dates = pd.date_range('2017-02-18',# 起始日期 periods=5,# 周期 freq='W-SAT') # 频率 print(dates) print(pd.Series(np.random.randn(5),index=dates))
print(pd.date_range(start='2017/02/18 12:13:14',periods=10)) print(pd.date_range(start='2017/02/18 12:13:14',normalize=True)) DatetimeIndex(['2017-02-18 12:13:14','2017-02-19 12:13:14','2017-02-20 12:13:14','2017-02-21 12:13:14','2017-02-22 12:13:14','2017-02-23 12:13:14','2017-02-24 12:13:14','2017-02-25 12:13:14','2017-02-26 12:13:14','2017-02-27 12:13:14'],dtype='datetime64[ns]',freq='D') DatetimeIndex(['2017-02-18','2017-02-19','2017-02-20','2017-02-21','2017-02-22','2017-02-23','2017-02-24','2017-02-25','2017-02-26','2017-02-27'],freq='D') 频率与偏移量
sum_offset = pd.tseries.offsets.Week(2) + pd.tseries.offsets.Hour(12) print(sum_offset) print(pd.date_range('2017/02/18','2017/03/18',freq=sum_offset)) 14 days 12:00:00 DatetimeIndex(['2017-02-18 00:00:00','2017-03-04 12:00:00'],freq='348H') 移动数据沿时间轴将数据前移或后移,保持索引不变 ts = pd.Series(np.random.randn(5),index=pd.date_range('20170218',periods=5,freq='W-SAT')) print(ts) 2017-02-18 -0.208622 2017-02-25 0.616093 2017-03-04 -0.424725 2017-03-11 -0.361475 2017-03-18 0.761274 Freq: W-SAT,dtype: float64 向后移动一位: 2017-02-18 NaN 2017-02-25 -0.208622 2017-03-04 0.616093 2017-03-11 -0.424725 2017-03-18 -0.361475 Freq: W-SAT,dtype: float64 pd.to_datetime()
功能:字符串转成时间格式
import pandas as pd s_obj = pd.Series(['2017/02/18','2017/02/19','2017-02-26'],name='course_time') s_obj2 = pd.to_datetime(s_obj) print(s_obj2)
# 处理缺失值 s_obj3 = pd.Series(['2017/02/18','2017-02-26'] + [None],name='course_time') print(s_obj3) 0 2017/02/18 1 2017/02/19 2 2017-02-25 3 2017-02-26 4 None Name: course_time,dtype: object 时间周期计算
2.2 时间数据重采样重采样(resampling)
import pandas as pd import numpy as np date_rng = pd.date_range('20170101',periods=100,freq='D') ser_obj = pd.Series(range(len(date_rng)),index=date_rng) # 统计每个月的数据总和 resample_month_sum = ser_obj.resample('M').sum() # 统计每个月的数据平均 resample_month_mean = ser_obj.resample('M').mean() print('按月求和:',resample_month_sum) print('按月求均值:',resample_month_mean) 按月求和: 2017-01-31 465 2017-02-28 1246 2017-03-31 2294 2017-04-30 945 Freq: M,dtype: int32 按月求均值: 2017-01-31 15.0 2017-02-28 44.5 2017-03-31 74.0 2017-04-30 94.5 Freq: M,dtype: float64 降采样(downsampling)
# 将数据聚合到5天的频率 five_day_sum_sample = ser_obj.resample('5D').sum() five_day_mean_sample = ser_obj.resample('5D').mean() five_day_ohlc_sample = ser_obj.resample('5D').ohlc()
升采样(upsampling)
2.3 滑动窗口
import pandas as pd import numpy as np ser_obj = pd.Series(np.random.randn(1000),index=pd.date_range('20170101',periods=1000)) ser_obj = ser_obj.cumsum() r_obj = ser_obj.rolling(window=5) print(r_obj)
设置居中: # 画图查看 import matplotlib.pyplot as plt %matplotlib inline plt.figure(figsize=(15,5)) ser_obj.plot(style='r--') ser_obj.rolling(window=10,center=True).mean().plot(style='b') 不设置居中: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |