Pandas时间序列
Pandas时间序列
pandas 提供了一组标准的时间序列处理工具和数据算法
数据类型及操作
Python 标准库的 datetime
datetime 模块中的 datetime、 time、 calendar 等类都可以用来存储时间类型以及进行一些转换和运算操作。
from datetime import datetime
now = datetime.now()
now delta = datetime(2010,2,2)-datetime(2010,2,1)
delta now + delta
datetime 对象间的减法运算会得到一个 timedelta 对象,表示一个时间段。
datetime 对象与它所保存的字符串格式时间戳之间可以互相转换。str() 函数是可用的,但更推荐 datetime.strptime() 方法。这个方法可以实现双向转换。
str(now)
now.strftime('%Y-%m-%d')
datetime.strptime('2010-01-01','%Y-%m-%d')
pandas 的 TimeStamp
pandas 最基本的时间日期对象是一个从 Series 派生出来的子类 TimeStamp,这个对象与 datetime 对象保有高度兼容性,可通过 pd.to_datetime() 函数转换。(一般是从 datetime 转换为 Timestamp)
pd.to_datetime(now) pd.to_datetime(np.nan)
pandas 的时间序列
pandas 最基本的时间序列类型就是以时间戳(TimeStamp)为 index 元素的 Series 类型。
dates = [datetime(2011,1,1),datetime(2011,1,2),datetime(2011,1,3)]
ts = pd.Series(np.random.randn(3),index=dates)
ts type(ts) ts.index ts.index[0]
时间序列之间的算术运算会自动按时间对齐。
索引、选取、子集构造
时间序列只是 index 比较特殊的 Series ,因此一般的索引操作对时间序列依然有效。其特别之处在于对时间序列索引的操作优化。如使用各种字符串进行索引:
ts[''] ts['2011-01-01'] ts['01/01/2011']
对于较长的序列,还可以只传入 “年” 或 “年月” 选取切片:
ts ts[''] ts['2011-1-2':'2012-12']
生成日期范围
pd.date_range() 可用于生成指定长度的 DatetimeIndex。参数可以是起始结束日期,或单给一个日期,加一个时间段参数。日期是包含的。
pd.date_range('','')
pd.date_range(start='',periods=10)
pd.date_range(end='',periods=10)
移动(超前和滞后)数据
移动(shifting)指的是沿着时间轴将数据前移或后移。Series 和 DataFrame 都有一个 .shift() 方法用于执行单纯的移动操作,index 维持不变:
ts ts.shift(2) ts.shift(-2)
因为移动操作产生了 NA 值,另一种移动方法是移动 index,而保持数据不变。这种移动方法需要额外提供一个 freq 参数来指定移动的频率:
ts.shift(2,freq='D') ts.shift(2,freq='3D')
时期及其算术运算
时期(period)概念不同于前面的时间戳(timestamp),指的是一个时间段。但在使用上并没有太多不同,pd.Period 类的构造函数仍需要一个时间戳,以及一个 freq 参数。freq 用于指明该 period 的长度,时间戳则说明该 period 在公园时间轴上的位置。
p = pd.Period(2010,freq='M')
p p + 2
上例中我给 period 的构造器传了一个 “年” 单位的时间戳和一个 “Month” 的 freq,pandas 便自动把 2010 解释为了 2010-01。
period_range 函数可用于创建规则的时间范围:
pd.period_range('2010-01','2010-05',freq='M')
PeriodIndex 类保存了一组 period,它可以在任何 pandas 数据结构中被用作轴索引:
pd.Series(np.random.randn(5),index=pd.period_range('','',freq='M'))
重采样
Pandas可以通过频率转换简单高效的进行重新采样
Pandas在对频率转换进行重新采样时拥有简单、强大且高效的功能(如将按秒采样的数据转换为按分钟为单位进行采样的数据)。这种操作在金融领域非常常见。
rng = pd.date_range('1/1/2012', periods=100, freq='S')
rng
ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)
ts
ts.resample('1Min').sum() #将秒级数据整合(加)成1min的数据
其他类型数值转为时间类型
时间字符串转时间格式:整型例如 20010100000000 这类格式容易当成时间戳转错,带format格式才行
pd.to_datetime(series时间字符,format='%Y%m%d%H%M%S') a = pd.DataFrame([[20010101,100000,'aaa'],[20010201,230100,'bbb']])
a pd.to_datetime(a[0],format='%Y%m%d')
Pandas时间序列的更多相关文章
- Pandas 时间序列处理
目录 Pandas 时间序列处理 1 Python 的日期和时间处理 1.1 常用模块 1.2 字符串和 datetime 转换 2 Pandas 的时间处理及操作 2.1 创建与基础操作 2.2 时 ...
- Python Pandas 时间序列双轴折线图
时间序列pv-gmv双轴折线图 import numpy as np import pandas as pd import matplotlib.pyplot as plt n = 12 date_s ...
- pandas时间序列滑窗
时间序列数据统计-滑动窗口 窗口函数 import pandas as pd import numpy as np ser_obj = pd.Series(np.random.randn(1000), ...
- Pandas 时间序列
# 导入相关库 import numpy as np import pandas as pd 在做金融领域方面的分析时,经常会对时间进行一系列的处理.Pandas 内部自带了很多关于时间序列相关的工具 ...
- Pandas时间序列和分组聚合
#时间序列import pandas as pd import numpy as np # 生成一段时间范围 ''' 该函数主要用于生成一个固定频率的时间索引,在调用构造方法时,必须指定start.e ...
- pandas时间序列常用操作
目录 一.时间序列是什么 二.时间序列的选取 三.时间序列的生成 四.时间序列的偏移量 五.时间前移或后移 五.时区处理 六.时期及算术运算 七.频率转换 一.时间序列是什么 时间序列在多个时间点观察 ...
- pandas时间序列学习笔记
目录 创建一个时间序列 pd.date_range() info() asfred() shifted(),滞后函数 diff()求差分 加减乘除 DataFrame.reindex() 通过data ...
- Python——Pandas 时间序列数据处理
介绍 Pandas 是非常著名的开源数据处理库,我们可以通过它完成对数据集进行快速读取.转换.过滤.分析等一系列操作.同样,Pandas 已经被证明为是非常强大的用于处理时间序列数据的工具.本节将介绍 ...
- pandas 时间序列resample
resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计 函数原型:DataFrame.resample(rule, how=None ...
随机推荐
- requests模块报错:Use body.encode('utf-8') if you want to send it encoded in UTF-8.
在做 企业向微信用户个人付款 功能时,调用第三方sdk,在 进行 requests 的post请求时, 代码如下 req = requests.post(url, data=data,cert(ap ...
- 【Jmeter自学】Jmeter里的指标
聚合报告 Average:平均响应时间(毫秒ms) Median:中值时间,N个数据从小到大排列,第N/2个数 9x%Line:N个数据从小到大排列,第9x%N个数.所有数据中9x%的响应时间都小于9 ...
- python中的ljust、rjust
ljust()将字符串左对齐右侧填充 rjust()将字符串右对齐左侧填充 举个例子: 1 a = "hello world" 2 a1 = a.ljust(15, "* ...
- Android Data Binding Library
Data Binding Library Data Binding Library是一个支持库,允许您使用声明格式(而不是编程)将布局中的UI组件与应用程序中的数据源绑定. 布局通常在调用UI框架方法 ...
- Flex学习笔记-时间触发器
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- CentOS7 安装 Mongodb 与 NodeJs 主要心得
一.mongodb 1.安装 由于使用yum源下载安装总是超时,所以选择了tarball方式安装. 官方安装方法链接,https://docs.mongodb.com/manual/tutorial/ ...
- Oracle服务无法启动,报:Windows无法启动OracleOraDb10g_home1TNSListener服务,错误 1067:进程意外终止。
运行配置和移植工具中的Net Configuration Assistant,进行监听程序配置.删除配置,然后重新配置. 切记 一定是先删除配置,再重新配置,而不是新建配置. 或者 打开Net Man ...
- yum centos 修改镜像源
参考:https://blog.csdn.net/sj349781478/article/details/78736873 3.清除yum缓存 yum clean all yum makecache ...
- sql server 语法 MSDN
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql
- JVM老年代和新生代的比例
在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ( Young ) 又被划分为三个区域:Eden.From Survivor.To Surviv ...