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. Kafka安装教程(详细过程)

    安装前期准备: 1,准备三个节点(根据自己需求决定) 2,三个节点上安装好zookeeper(也可以使用kafka自带的zookeeper) 3,关闭防火墙 chkconfig  iptables o ...

  2. 配置HTTPS全过程

    HTTPS配置全过程服务器配置https协议HTTPS,是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL. ...

  3. nginx 安装 ssl 证书

    nginx 安装 ssl 证书 关键词: pem 转 crt , 证书续期, nginx 部署 ssl 证书, 解决 SSL23_GET_SERVER_HELLO 错误. 之前免费申请的 1年的证书过 ...

  4. PHP 指定时间/时间戳+某天/某月/某年

    PHP指定时间戳加上1天,1周,1月,一年其实是不需要用上什么函数的!指定时间戳本身就是数字整型,我们只需要再计算1天,1周它的秒数相加即可! 博主搜索php指定时间戳加一天一年,结果许多的文章给出来 ...

  5. 报错:pymysql.err.InternalError: (1054, "Unknown column 'AType' in 'field list'")

    报错背景: 报错前sql代码展示: List = ['] # sql = "insert into test(id, name) value ("+"'"+ L ...

  6. eclipse 中的注释 快捷键 多行注释快捷键 单行注释快捷键

    本文链接:https://blog.csdn.net/a0701302/article/details/76177244 Eclipse 中的两种注释方法: (1)多行注释 (2)单行注释 一. 多行 ...

  7. HTML5 VUE单页应用 SEO 优化之 预渲染(prerender-spa-plugin)

    前言:当前 SPA 架构流行的趋势如日中天,前后端分离的业务模式已经成为互联网开发的主流方式,但是 单页面 应用始终存在一个痛点,那就是 SEO, 对于那些需要推广,希望能在百度搜索时排名靠前的网站而 ...

  8. php代码规范->如何写出规范且易于理解的项目代码-ZX版

    2019年5月17日10:50:12 前序: 目前是想到哪写到哪,后面有时间在整理成具体文章 很多时候,PHP代码风格过于自由,导致一个项目有N多种写法风格,有些人为了自己认为的技术"高&q ...

  9. [转]java 根据模板文件生成word文档

    链接地址:https://blog.csdn.net/ai_0922/article/details/82773466

  10. express脚手架重建node项目

    安装express 和express-generator cnpm install express express-generator -g express demo1 创建demo1项目, 进入项目 ...