Python--时间序列.s(索引、切片、重采样)

时间序列    索引 / 切片 / 重采样


时间序列  ?? 索引

# 示例数据
import numpy as np
import pandas as pd
import datetime

times = pd.date_range(‘2019-1-1‘,periods=10,freq=‘MS‘)
ps = pd.Series(np.random.rand(len(times)),index=times)

#-----输出-----#
2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
2019-04-01    0.509028
2019-05-01    0.943419
2019-06-01    0.619618
2019-07-01    0.736451
2019-08-01    0.695320
2019-09-01    0.607416
2019-10-01    0.506309
Freq: MS, dtype: float64

索引 (整数索引,索引和列表一样没有区别。) :

ps[0]
ps[::2]
ps[:3]
ps[‘2019‘]
ps[‘2019-1‘]

#-----输出-----#
0.3741804976952492

2019-01-01    0.374180
2019-03-01    0.098253
2019-05-01    0.943419
2019-07-01    0.736451
2019-09-01    0.607416

2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
Freq: MS, dtype: float64

2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
2019-04-01    0.509028
2019-05-01    0.943419
2019-06-01    0.619618
2019-07-01    0.736451
2019-08-01    0.695320
2019-09-01    0.607416
2019-10-01    0.506309
Freq: MS, dtype: float64

2019-01-01    0.37418
Freq: MS, dtype: float64

[datetime.datetime(2019,1,1)]

#-----输出-----#
0.3741804976952492

?? 时间序列索引,支持各种字符串 及 datetime 对象


时间序列 ?? 切片

ps[‘2019-1‘:‘2019-5‘]
ps[‘2019-1‘:‘2019-5‘:2]

#-----输出-----#
2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
2019-04-01    0.509028
2019-05-01    0.943419
Freq: MS, dtype: float64

2019-01-01    0.374180
2019-03-01    0.098253
2019-05-01    0.943419
Freq: 2MS, dtype: float64

?? 切片 和 Series索引标签切片原理一致,是一个闭区间,包头尾



重复索引的时间序列

tid = pd.DatetimeIndex([‘2019-10-1‘,‘2019-10-2‘,‘2019-10-2‘,‘2019-10-4‘,‘2019-10-5‘,‘2019-10-6‘])
ps = pd.Series(np.random.rand(len(tid)),index=tid)

#-----输出-----#
2019-10-01    0.740345
2019-10-02    0.087693
2019-10-02    0.710417
2019-10-04    0.140575
2019-10-05    0.834221
2019-10-06    0.312474
dtype: float64

?? .is_unique : 检查值是否唯一  // \\ .index.is_unique : 检查索引是否唯一

ps.index.is_unique
#-----输出----#
False

ps.is_unique
#-----输出----#
True

# 如果索引值重复,则返回多个值 ,如下 :

ps[‘2019-10-2‘]

#-----输出-----#
2019-10-02    0.087693
2019-10-02    0.710417
dtype: float64

时间序列 ?? 重采样   .resample()

将时间序列从一个频率转换为另一个频率的过程

降采样 , 高频数据-->低频数据

升采样 , 低频数据-->高频数据

# 示例数据
rs = pd.date_range(‘2019-1-1‘,periods=12,freq="MS")
ts = pd.Series(np.arange(len(rs)),index=rs)

#-----输出-----#
2019-01-01     0
2019-02-01     1
2019-03-01     2
2019-04-01     3
2019-05-01     4
2019-06-01     5
2019-07-01     6
2019-08-01     7
2019-09-01     8
2019-10-01     9
2019-11-01    10
2019-12-01    11
Freq: MS, dtype: int32

每2个月进行值求和 :

ts = ts.resample(‘2MS‘).sum()  

#-----输出-----#
2019-01-01     1
2019-03-01     5
2019-05-01     9
2019-07-01    13
2019-09-01    17
2019-11-01    21
Freq: 2MS, dtype: int32

##  生成一个重采样的构建器 ,频率改为2MS 2个月初
#  .sum() 得到一个重新聚合后的Series,聚合方式时求和
#  降采样,需要聚合
#  mean() / max() / min() / median() 中值 / first() / last() / ohlc() 重采样
#  OHLC : 金融领域的时间序列聚合方式 → open 开盘 high 最大值 low 最小值 close 收盘

ts .resample(‘2MS‘,closed=‘right‘).sum()  # 每隔2行数据 , 下2个日期相加之和
ts.resample(‘2MS‘,closed=‘left‘).sum()  #每隔2行数据,当前值+下一个值之和
#具体作用 我是新手 我也不懂

#-----输出-----#
2018-11-01     0
2019-01-01     3
2019-03-01     7
2019-05-01    11
2019-07-01    15
2019-09-01    19
2019-11-01    11
Freq: 2MS, dtype: int32

2019-01-01     1
2019-03-01     5
2019-05-01     9
2019-07-01    13
2019-09-01    17
2019-11-01    21
Freq: 2MS, dtype: int32

升采样 :

ts.resample(‘H‘)

# 生成一个重采样的构建器
# 从低频转高频,主要是如何插值

DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, label=left, convention=start, base=0]

ts.resample(‘H‘).ffill()   # .ffill() 向前填充
ts.resample(‘H‘).bfill()   # .bfill() 向后填充
ts.resample(‘H‘).asfreq()   # .asfreq() 不做填充 返回NaN

# 值填充  数据太长了 , 不展示 #

时期的重采样 :

pp = pd.period_range(‘2018‘,‘2019‘,freq=‘M‘)
ts = pd.Series(np.arange(len(pp)),index=pp)

#-----示例-----#
2018-01     0
2018-02     1
2018-03     2
2018-04     3
2018-05     4
2018-06     5
2018-07     6
2018-08     7
2018-09     8
2018-10     9
2018-11    10
2018-12    11
2019-01    12
Freq: M, dtype: int32

对于时期的降采样,目标频率要是原频率的超时期 :

ts.resample(‘Q‘).sum()  #降采样
ts.resample(‘D‘).ffill()   #升采样

运行结果 :

2018Q1     3
2018Q2    12
2018Q3    21
2018Q4    30
2019Q1    12
Freq: Q-DEC, dtype: int32

2018-01-01     0
2018-01-02     0
2018-01-03     0
2018-01-04     0
2018-01-05     0
2018-01-06     0
2018-01-07     0
2018-01-08     0
2018-01-09     0
2018-01-10     0
2018-01-11     0
2018-01-12     0
2018-01-13     0
2018-01-14     0
2018-01-15     0
2018-01-16     0
2018-01-17     0
2018-01-18     0
2018-01-19     0
2018-01-20     0
2018-01-21     0
2018-01-22     0
2018-01-23     0
2018-01-24     0
2018-01-25     0
2018-01-26     0
2018-01-27     0
2018-01-28     0
2018-01-29     0
2018-01-30     0
              ..
2019-01-02    12
2019-01-03    12
2019-01-04    12
2019-01-05    12
2019-01-06    12
2019-01-07    12
2019-01-08    12
2019-01-09    12
2019-01-10    12
2019-01-11    12
2019-01-12    12
2019-01-13    12
2019-01-14    12
2019-01-15    12
2019-01-16    12
2019-01-17    12
2019-01-18    12
2019-01-19    12
2019-01-20    12
2019-01-21    12
2019-01-22    12
2019-01-23    12
2019-01-24    12
2019-01-25    12
2019-01-26    12
2019-01-27    12
2019-01-28    12
2019-01-29    12
2019-01-30    12
2019-01-31    12
Freq: D, Length: 396, dtype: int32

## 我也不懂,请自行 “百度” ## 

原文地址:https://www.cnblogs.com/luowei93/p/11815966.html

时间: 2024-07-31 14:50:10

Python--时间序列.s(索引、切片、重采样)的相关文章

Python array,list,dataframe索引切片操作 2016年07月19日——智浪文档

array,list,dataframe索引切片操作 2016年07月19日——智浪文档 list,一维,二维array,datafrme,loc.iloc.ix的简单探讨 Numpy数组的索引和切片介绍: 从最基础的list索引开始讲起,我们先上一段代码和结果: a = [0,1,2,3,4,5,6,7,8,9] a[:5:-1] #step < 0,所以start = 9 a[0:5:-1] #指定了start = 0 a[1::-1] #step < 0,所以stop = 0 输出: [

python之路-利用索引切片功能做一个简易的两个未知数的加法计算器,代码如下:

python之路-利用索引切片功能做一个简易的两个未知数的加法计算器,代码如下: #content = input('请输入内容:'),如用户输入:5 +9或 5 + 9 等,然后进行分割再进行计算. content = input('>>>').strip() #content 等于所输入的内容,strip:删除字符串左右两边的空格. index = content.find('+') ''' content内容中的两边的空格都删除了,但中间还有,现在我们只需要定位已知内容是加法,两边

初学 Python(十一)——切片

初学 Python(十一)--切片 初学 Python,主要整理一些学习到的知识点,这次是切片. #-*- coding:utf-8 -*- ''''' 切片 ''' L = ['name','age','sex','address','company'] #取前2个 print L[0:2] print L[:2] #取倒数第一个 print L[-1] #取后两个 print L[-2:] #取倒数第二个 print L[-2:-1] print len(L) #隔一个数取一次,从第一个数开

python笔记八(切片)

一.切片 首先我们要记得在Python中可以用于切片的对象有 列表.元组.字符串. 切片操作就是直接从列表.元组或字符串中,选择出我们想要的内容,这些操作非常简洁实用. >>> L = list(range(20)) >>> L [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> L[0:10] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

NumPy索引切片

索引,切片和迭代 一维数组可以被索引,切片和迭代,就像 列表 和其他Python序列一样. 代码实例解析数组中的索引切片 >>> import numpy as np   #导入numpy 别名为np>>> a = np.arange(10)**3>>> aarray([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])>>> a[2]                        

列表的初识,列表的索引切片,列表的增删改查,列表的嵌套,元组的初识,range

1 内容总览 列表的初识 列表的索引切片 列表的增删改查 列表的嵌套 元组的初识(了解) 元组的简单应用(了解) range 2 具体内容 列表的初识 why: str: 存储少量的数据.切片出来全都是str类型,存储的数据单一. list:能储存大量的数据.包含不同类型的数据.且有顺序,有规律,可自己制作设计其中的数据,可修改 what:list l1 = [100, 'alex',True,[1, 2, 3]] 可承载任意数据类型,存储大量的数据. python常用的容器型数据类型. 列表是

5-字符串传值%s、format、索引切片、字符串处理

%s 传入变量值string %d 传入变量值为int %f  传入变量值为float format方式  或者 单引号 '   '                    单行 双引号 "  "                    单行 三引号 """     """           多行 索引,切片 print name[1:2]  #从第2个值开始,每隔一个值取一次 strip()去除字符串中的空格 strip('\

pytorch张量数据索引切片与维度变换操作大全(非常全)

(1-1)pytorch张量数据的索引与切片操作1.对于张量数据的索引操作主要有以下几种方式:a=torch.rand(4,3,28,28):DIM=4的张量数据a(1)a[:2]:取第一个维度的前2个维度数据(不包括2):(2)a[:2,:1,:,:]:取第一个维度的前两个数据,取第2个维度的前1个数据,后两个维度全都取到:(3)a[:2,1:,:,:]:取第一个维度的前两个数据,取第2个维度的第1个索引到最后索引的数据(包含1),后两个维度全都取到:(4)a[:2,-3:]:负号表示第2个维

Python&amp;int&amp;method&amp;String切片、索引,列表、元祖、字典

一.int的两个方法 a. __add__() bit_length() number_one = 7number_two = 5print(number_one + number_two)print(number_one.__add__(number_two))print(number_one.bit_length())print(number_two.bit_length()) 效果: 121233 二.String索引和切片 name = 'Jane'print(name[0])print

Python的学习之-切片和索引

字符串的切片 切片格式:s[start_index:end_index:strp]: 表示对变量s进行切片索引,start_index表示起始位置,end_index表示结束位置,strp则表示步长 PS:注意索引时候因为是从0开始,所以0-7显示的对应值其实就是0-6的值.参考索引位置 例:顺序切片 s=abcdefghijklmn s[0:7:2]   #则表示从0-7,步长2,就是从每两个位置取一个值,输出则是aceg 例:反转切片 s=abcdefghijklmn s[::-1]   #