python+pandas生成指定日期和重采样
来源:互联网 发布:工程咨询资质取消知乎 编辑:程序博客网 时间:2024/05/21 14:07
python 日期的范围、频率、重采样以及频率转换
pandas有一整套的标准时间序列频率以及用于重采样、频率推断、生成固定频率日期范围的工具。
生成指定日期范围的范围
pandas.date_range()用于生成指定长度的DatatimeIndex:
1)默认情况下,date_range会按着时间间隔为天的方式生成从给定开始到结束时间的时间戳数组;
2)如果只指定开始或结束时间,还需要periods标定时间长度。
import pandas as pdpd.date_range('2017-6-20','2017-6-27')
DatetimeIndex(['2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23', '2017-06-24', '2017-06-25', '2017-06-26', '2017-06-27'], dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-20 12:59:30','2017-6-27')
DatetimeIndex(['2017-06-20 12:59:30', '2017-06-21 12:59:30', '2017-06-22 12:59:30', '2017-06-23 12:59:30', '2017-06-24 12:59:30', '2017-06-25 12:59:30', '2017-06-26 12:59:30'], dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-20 12:59:30',periods = 8)
DatetimeIndex(['2017-06-20 12:59:30', '2017-06-21 12:59:30', '2017-06-22 12:59:30', '2017-06-23 12:59:30', '2017-06-24 12:59:30', '2017-06-25 12:59:30', '2017-06-26 12:59:30', '2017-06-27 12:59:30'], dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-20 12:59:30',periods = 8, normalize = True)
DatetimeIndex(['2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23', '2017-06-24', '2017-06-25', '2017-06-26', '2017-06-27'], dtype='datetime64[ns]', freq='D')
频率和日期偏移量
pandas中的频率是由一个基础频率(M、H)也可以是(Hour、Minute、h、min等)
pd.date_range('2017-6-27',periods = 7,freq = '1h30min')
DatetimeIndex(['2017-06-27 00:00:00', '2017-06-27 01:30:00', '2017-06-27 03:00:00', '2017-06-27 04:30:00', '2017-06-27 06:00:00', '2017-06-27 07:30:00', '2017-06-27 09:00:00'], dtype='datetime64[ns]', freq='90T')
pd.date_range('2017-6-27',periods = 7,freq = 'M')
DatetimeIndex(['2017-06-30', '2017-07-31', '2017-08-31', '2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31'], dtype='datetime64[ns]', freq='M')
pd.date_range('2017-6-27',periods = 7,freq = 'd')
DatetimeIndex(['2017-06-27', '2017-06-28', '2017-06-29', '2017-06-30', '2017-07-01', '2017-07-02', '2017-07-03'], dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-27',periods = 7,freq = 'H')
DatetimeIndex(['2017-06-27 00:00:00', '2017-06-27 01:00:00', '2017-06-27 02:00:00', '2017-06-27 03:00:00', '2017-06-27 04:00:00', '2017-06-27 05:00:00', '2017-06-27 06:00:00'], dtype='datetime64[ns]', freq='H')
常用的基础频率
上表只展示了部分!
WOM日期(可获得例如“每月第3个星期五”)
pd.date_range('2017-06-01','2017-07-31',freq='WOM-3FRI')
DatetimeIndex(['2017-06-16', '2017-07-21'], dtype='datetime64[ns]', freq='WOM-3FRI')
重采样及频率转换
降采样:高频数据到低频数据
升采样:低频数据到高频数据
主要函数:resample()(pandas对象都会有这个方法)
resample方法的参数
降采样
需考虑:
1)各区间哪边是闭合的(参数:closed)
2)如何标记各聚合面元,用区间的开头还是末尾(参数:label)
ts_index = pd.date_range('2017-06-20',periods =12,freq = '1min')#一分钟采样数据ts = pd.Series(np.arange(12),index = ts_index)
ts
2017-06-20 00:00:00 0 2017-06-20 00:01:00 1 2017-06-20 00:02:00 2 2017-06-20 00:03:00 3 2017-06-20 00:04:00 4 2017-06-20 00:05:00 5 2017-06-20 00:06:00 6 2017-06-20 00:07:00 7 2017-06-20 00:08:00 8 2017-06-20 00:09:00 9 2017-06-20 00:10:00 10 2017-06-20 00:11:00 11 Freq: T, dtype: int32
聚合到5分钟
ts.resample('5min',how='sum')
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated the new syntax is .resample(...).sum() if __name__ == '__main__': 2017-06-20 00:00:00 10 2017-06-20 00:05:00 35 2017-06-20 00:10:00 21 Freq: 5T, dtype: int32
ts.resample('5min',how='sum',closed='left')
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated the new syntax is .resample(...).sum() if __name__ == '__main__': 2017-06-20 00:00:00 10 2017-06-20 00:05:00 35 2017-06-20 00:10:00 21 Freq: 5T, dtype: int32
ts.resample('5min',how='sum',closed='left',label ='left')
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated the new syntax is .resample(...).sum() if __name__ == '__main__': 2017-06-20 00:00:00 10 2017-06-20 00:05:00 35 2017-06-20 00:10:00 21 Freq: 5T, dtype: int32
通过groupby进行重插样
另外一种降采样方法
ts1_index = pd.date_range('2017-6-01',periods = 100,freq = 'd')ts1 = pd.Series(np.arange(100),index = ts1_index)ts1.head()
2017-06-01 0 2017-06-02 1 2017-06-03 2 2017-06-04 3 2017-06-05 4 Freq: D, dtype: int32
ts1.groupby(lambda x:x.month).mean()
6 14.5 7 45.0 8 76.0 9 95.5 dtype: float64
ts1.groupby(lambda x:x.weekday).mean()
0 49.5 1 50.5 2 51.5 3 49.0 4 50.0 5 47.5 6 48.5 dtype: float64
df1 = pd.DataFrame(np.arange(200).reshape(100,2),index = ts1_index)
df1.groupby(lambda x:x.weekday).mean()
对于具有时间序列索引的pandas数据结构,当groupby传入一个函数时,可以对时间索引对应列进行聚合
升采样
升采样没有聚合,但是需要填充
df2 = pd.DataFrame(np.arange(200).reshape(100,2),index = ts1_index,columns=['add1','add2'])df2.head()
df2.resample('W-THU',fill_method = 'ffill')
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: fill_method is deprecated to .resample() the new syntax is .resample(...).ffill() if __name__ == '__main__':
总结
本篇博客主要内容:
1)生成指定时间段,指定频率的日期
2)对含有时间索引的pandas数据进行重采样,包括降采样和升采样等。
阅读全文
1 0
- python+pandas生成指定日期和重采样
- python科学计算笔记(十一)pandas中date_range生成指定日期
- python pandas 自动填充,重新采样 resample
- 指定起始和结束日期循环生成中间所有日期
- python科学计算笔记(十二)pandas的resample采样
- python pandas 序列的生成
- Python图像批处理(图像重采样)
- 基于Python 的语音重采样函数
- 重玩python,安装pandas pandas pycharm pip一堆工具
- python pandas dataframe 去重函数
- python pandas dataframe 去重函数
- python pandas dataframe 去重函数
- 重采样
- 重采样
- 金字塔和重采样的解析
- pandas 生成excel 和 csv
- 根据指定 日期格式 生成 指定格式的指定日期
- 指定日期段生成代码
- Java泛型小结
- Android stduio之Plugin with id 'com.android.application' not found解决办法
- Java Socket搭建一个简单地Web服务器(1)
- 利用StringEscapeUtils对字符串进行各种转义与反转义(Java)
- 浅析TCP三次握手和四次挥手
- python+pandas生成指定日期和重采样
- HibernateUtils工具---【小白系列】0基础到熟练应用hibernate框架(四)
- mysql绿色安装
- Spring源码解析之四 ------ AOP原理和源码分析
- Java学习笔记-《Java程序员面试宝典》-第5章Java Web-5.1Servlet与JSP(5.1.1-5.1.7)
- windows32 openssl-0.9.8a安装步骤
- 如何再Win10系统下面配置Docker的国内镜像站?
- CSDN、中国科学院自动化研究所承办的第三届中国人工智能大会
- 使用nmap 验证多种漏洞