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时间序列的更多相关文章

  1. Pandas 时间序列处理

    目录 Pandas 时间序列处理 1 Python 的日期和时间处理 1.1 常用模块 1.2 字符串和 datetime 转换 2 Pandas 的时间处理及操作 2.1 创建与基础操作 2.2 时 ...

  2. Python Pandas 时间序列双轴折线图

    时间序列pv-gmv双轴折线图 import numpy as np import pandas as pd import matplotlib.pyplot as plt n = 12 date_s ...

  3. pandas时间序列滑窗

    时间序列数据统计-滑动窗口 窗口函数 import pandas as pd import numpy as np ser_obj = pd.Series(np.random.randn(1000), ...

  4. Pandas 时间序列

    # 导入相关库 import numpy as np import pandas as pd 在做金融领域方面的分析时,经常会对时间进行一系列的处理.Pandas 内部自带了很多关于时间序列相关的工具 ...

  5. Pandas时间序列和分组聚合

    #时间序列import pandas as pd import numpy as np # 生成一段时间范围 ''' 该函数主要用于生成一个固定频率的时间索引,在调用构造方法时,必须指定start.e ...

  6. pandas时间序列常用操作

    目录 一.时间序列是什么 二.时间序列的选取 三.时间序列的生成 四.时间序列的偏移量 五.时间前移或后移 五.时区处理 六.时期及算术运算 七.频率转换 一.时间序列是什么 时间序列在多个时间点观察 ...

  7. pandas时间序列学习笔记

    目录 创建一个时间序列 pd.date_range() info() asfred() shifted(),滞后函数 diff()求差分 加减乘除 DataFrame.reindex() 通过data ...

  8. Python——Pandas 时间序列数据处理

    介绍 Pandas 是非常著名的开源数据处理库,我们可以通过它完成对数据集进行快速读取.转换.过滤.分析等一系列操作.同样,Pandas 已经被证明为是非常强大的用于处理时间序列数据的工具.本节将介绍 ...

  9. pandas 时间序列resample

    resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计 函数原型:DataFrame.resample(rule, how=None ...

随机推荐

  1. json与bson的区别

    bson是由10gen开发的一个数据格式,目前主要用于mongoDB中,是mongoDB的数据存储格式.bson基于json格式,选择json进行改造的原因主要是json的通用性及json的schem ...

  2. linux&php:ubuntu安装php-7.2

    1.下载php源码,地址:http://www.php.net/downloads.php 这里下载的是tar.gz的包 2.解压安装 将安装包解压到/usr/local/php 安装C的编译工具 s ...

  3. myeclipse内存调整

    内存调整: myeclipse.ini里配置后 1.设置Default VM Arguments 在myEclipse中,打开Windows-> Preferences->Java-> ...

  4. 分布式计算课程补充笔记 part 1

    ▶ 高性能计算机发展历程 真空管电子计算机,向量机(Vector Machine),并行向量处理机(Parallel Vector Processors,PVP),分布式并行机(Parallel Pr ...

  5. [多线程]wait和notify

    线程之间的通信   使用wait/notify方法实现线程间的通信.这两个方法都是Object类的方法,也就是说Java所有的对象都提供这两个方法.1.wait和notify必须配合synchroni ...

  6. mysql 外键引发的删除失败

    mysql> TRUNCATE TABLE role ; ERROR 1701 (42000): Cannot truncate a table referenced in a foreign ...

  7. bash-4.2$ bash: /home/test/.bashrc: 权限不够

    解决办法:https://blog.csdn.net/qq_38417808/article/details/81705559

  8. 使用MediaPlayer类和SurfaceView来播放视频

    MediaPlayer可以播放视频,只需需要SurfaceView的配合,SurfaceView主要用于显示MediaPlayer播放的视频流媒体的画面渲染. SurfaceView是配合MediaP ...

  9. Windows关闭开机自启项

    https://zhidao.baidu.com/question/562559980.html

  10. centos7+hadoop完全分布式集群搭建

    Hadoop集群部署,就是以Cluster mode方式进行部署.本文是基于JDK1.7.0_79,hadoop2.7.5.  1.Hadoop的节点构成如下: HDFS daemon:  NameN ...