pandas-13 时间序列操作方法pd.date_range()

在pandas中拥有强大的时间序列操作方法。

使用 pd.date_range() 生成 ‘pandas.core.indexes.datetimes.DatetimeIndex’ 对象。

直接上demo:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
from datetime import datetime np.random.seed(666) # 生成时间对象
t1 = datetime(2009, 10, 1)
print(type(t1)) # 2009-10-01 00:00:00 type:<class 'datetime.datetime'> date_list = [
datetime(2018, 10, 1),
datetime(2018, 10, 10),
datetime(2019, 10, 1),
datetime(2019, 10, 20),
datetime(2019, 11, 1)
] print(date_list)
# [datetime.datetime(2018, 10, 1, 0, 0), datetime.datetime(2018, 10, 10, 0, 0), datetime.datetime(2019, 10, 1, 0, 0), datetime.datetime(2019, 10, 20, 0, 0), datetime.datetime(2019, 11, 1, 0, 0)] s1 = Series(np.random.rand(5), index=date_list)
print(s1) # index 是 时间 对象
'''
2018-10-01 0.700437
2018-10-10 0.844187
2019-10-01 0.676514
2019-10-20 0.727858
2019-11-01 0.951458
dtype: float64
''' # 直接按照索引进行访问
print(s1[3]) # 0.7278580572480748 # 传入时间对象访问
print(s1[datetime(2019, 10, 20)]) # 0.7278580572480748 # 传入字符串
print(s1['2019-10-20']) # 0.7278580572480748 # 传入字符串2
print(s1['20191020']) # 0.7278580572480748 # 单纯写 年 月, 可以得到该年月下的所有内容
print(s1['2019-10'])
'''
2019-10-01 0.676514
2019-10-20 0.727858
dtype: float64
'''
# 原理同上
print(s1['2019'])
'''
2019-10-01 0.676514
2019-10-20 0.727858
2019-11-01 0.951458
dtype: float64
''' # date_range() 参数 start 开始时间, periods 间隔时间,freq 按照什么间隔 d w 5h……
date_list_new = pd.date_range(start='2018-01-1', periods=50, freq='w')
print(type(date_list_new), date_list_new) # <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
''' 一周 默认 从 周日 开始的, 如果需要更改,可以改变 freq='w-mon'
DatetimeIndex(['2018-01-07', '2018-01-14', '2018-01-21', '2018-01-28',
'2018-02-04', '2018-02-11', '2018-02-18', '2018-02-25',
'2018-03-04', '2018-03-11', '2018-03-18', '2018-03-25',
'2018-04-01', '2018-04-08', '2018-04-15', '2018-04-22',
'2018-04-29', '2018-05-06', '2018-05-13', '2018-05-20',
'2018-05-27', '2018-06-03', '2018-06-10', '2018-06-17',
'2018-06-24', '2018-07-01', '2018-07-08', '2018-07-15',
'2018-07-22', '2018-07-29', '2018-08-05', '2018-08-12',
'2018-08-19', '2018-08-26', '2018-09-02', '2018-09-09',
'2018-09-16', '2018-09-23', '2018-09-30', '2018-10-07',
'2018-10-14', '2018-10-21', '2018-10-28', '2018-11-04',
'2018-11-11', '2018-11-18', '2018-11-25', '2018-12-02',
'2018-12-09', '2018-12-16'],
dtype='datetime64[ns]', freq='W-SUN')
''' t_range = pd.date_range('2018-1-1', '2018-12-31')
print(t_range)
'''
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08',
'2018-01-09', '2018-01-10',
...
'2018-12-22', '2018-12-23', '2018-12-24', '2018-12-25',
'2018-12-26', '2018-12-27', '2018-12-28', '2018-12-29',
'2018-12-30', '2018-12-31'],
dtype='datetime64[ns]', length=365, freq='D')
''' s1 = Series(np.random.randn(len(t_range)), index=t_range)
print(s1) # 按照每月采样
# 可以求每个月的平均值,然后生成一个series
print(s1['2018-1'].mean()) # -0.03690388489355985 # 但是有更简单的方法
s1_month = s1.resample('M').mean()
print(s1_month)
'''
2018-01-31 -0.036904
2018-02-28 -0.044257
2018-03-31 0.037668
2018-04-30 0.124246
2018-05-31 -0.119733
2018-06-30 0.214724
2018-07-31 -0.123569
2018-08-31 0.180736
2018-09-30 -0.113882
2018-10-31 -0.111971
2018-11-30 -0.232061
2018-12-31 0.214558
Freq: M, dtype: float64
'''
print(s1.resample('H').ffill()) # 按小时采样,使用向前填充的方法
print(s1.resample('H').bfill()) # 向后填充

pandas-13 时间序列操作方法pd.date_range()的更多相关文章

  1. pandas处理时间序列(2):DatetimeIndex、索引和选择、含有重复索引的时间序列、日期范围与频率和移位、时间区间和区间算术

    一.时间序列基础 1. 时间戳索引DatetimeIndex 生成20个DatetimeIndex from datetime import datetime dates = pd.date_rang ...

  2. pandas处理时间序列(1):pd.Timestamp()、pd.Timedelta()、pd.datetime( )、 pd.Period()、pd.to_timestamp()、datetime.strftime()、pd.to_datetime( )、pd.to_period()

      Pandas库是处理时间序列的利器,pandas有着强大的日期数据处理功能,可以按日期筛选数据.按日期显示数据.按日期统计数据.   pandas的实际类型主要分为: timestamp(时间戳) ...

  3. 03. Pandas 2| 时间序列

    1.时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() 日期解析方法:pa ...

  4. pandas处理时间序列(3):重采样与频率转换

    五.重采样与频率转换 1. resample方法 rng = pd.date_range('1/3/2019',periods=1000,freq='D') rng 2. 降采样 (1)resampl ...

  5. pandas之时间序列

    Pandas中提供了许多用来处理时间格式文本的方法,包括按不同方法生成一个时间序列,修改时间的格式,重采样等等. 按不同的方法生成时间序列 In [7]: import pandas as pd # ...

  6. pandas 之 时间序列索引

    import numpy as np import pandas as pd 引入 A basic kind of time series object in pandas is a Series i ...

  7. pandas之时间序列笔记

    时间戳tiimestamp:固定的时刻->pd.Timestamp 固定时期period:比如2016年3月份,再如2015年销售额->pd.Period 时间间隔interval:由起始 ...

  8. pandas之时间序列(data_range)、重采样(resample)、重组时间序列(PeriodIndex)

    1.data_range生成时间范围 a) pd.date_range(start=None, end=None, periods=None, freq='D') start和end以及freq配合能 ...

  9. 笔记 | pandas之时间序列学习随笔1

    1. 时间序列自动生成 ts = pd.Series(np.arange(1, 901), index=pd.date_range('2010-1-1', periods=900)) 最终生成了从20 ...

随机推荐

  1. php – 通过curl从url获取JSON数据

    我试图通过curl连接从URL获取JSON数据.当我打开链接时:它显示{“version”:“N / A”,“success”:true,“status”:true}.现在,我希望获得以上内容. 到目 ...

  2. Linux虚拟机:发布WebService接口出现异常,无法访问接口

    Linux虚拟机:发布WebService接口出现异常,无法访问接口 今天在部署WebService工程的时候遇到的问题: 在Linux虚拟机上部署一个tomcat同时在tomcat下放置2个工程,其 ...

  3. iOS逆向(五)-ipa包重签名

    为什么要重签名? 1.在没有源代码的情况下,你已经对某个应用进行了资源修改(比如修改了启动图或图标等).修改完成以后,如果想要让APP可以正常使用,该APP一定要重新签名然后压缩成IPA文件. 2.如 ...

  4. SNF-软件开发机器人-免费-火爆登场-程序下载及实战配套教程免费发放

    软件开发机器人不辱使命的完成了在软件开发方面的方式方法,颠覆了传统开发,可零编程开发软件,也可二开更强大功能. 为了更好的了解和理解软件开发机器人我们以模拟用友u8系统部分供应链程序为例进行模拟. 联 ...

  5. SpringBoot 上下文获取注入的Bean

    import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...

  6. LeetCode 101. Symmetric Tree(镜像树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  7. Qt开发经验小技巧61-70

    很多人问Qt嵌入式平台用哪个好,这里统一回答(当前时间节点2018年):imx6+335x比较稳定,性能高就用RK3288 RK3399,便宜的话就用全志H3,玩一玩可以用树莓派香橙派. 对于大段的注 ...

  8. Elasticsearch学习笔记——索引模板

    在索引模板里面,date类型的字段的format支持多种类型,在es中全部会转换成long类型进行存储,参考 https://zhuanlan.zhihu.com/p/34240906 一个索引模板范 ...

  9. 在nginx环境下搭建基于ssl证书的websocket服务转发,wss

    1.证书准备 本地调试,可以安装自签名证书,安装方法参考https本地自签名证书添加到信任证书访问 2.修改配置文件 将上面的配置文件拷贝到conf目录,添加或者修改节点如下 # HTTPS serv ...

  10. [Golang] Gin框架学习笔记

    0x0 Gin简介 1.Gin 是什么? Gin 是一个用 Go (Golang) 编写的 HTTP web 框架. 它是一个类似于 martini 但拥有更好性能的 API 框架, 由于 httpr ...