内容承接上一篇文章,本文章主要介绍时间序列数据的相关Pandas知识。
一、时间模块
时间模块指的是datetime,主要需要学习datetime.date(),datetime.datetime(),datetime.timedelta()。
import numpy as np
import pandas as pd
import datetime
today=datetime.date.today()
print(today,type(today))
now=datetime.datetime.now()
print(now,type(now))
print(datetime.datetime(2016,6,1))
print(datetime.datetime(2016,6,1,15,30,25))
# datetime.timedelta时间差
t1=datetime.datetime(2000,10,1)
tx=datetime.timedelta(100)
print(t1+tx)
print(t1-tx)
# 字符串转换方法 parser.parse
from dateutil.parser import parse
# from numpy import random
date='12-21-2017'
date2='21/12/2017'
print(parse(date),type(parse(date)))
print(parse(date2))
二、时刻数据
1、Pandas时刻数据:Timestamp
时刻数据代表时间点,是pandas的数据类型,是将值与时间点相关联的最基本类型的时间序列数据。
import numpy as np
import pandas as pd
import datetime
date1='20200101'
date2=datetime.datetime(2020,1,1,15,0)
t1=pd.Timestamp(date1)
t2=pd.Timestamp(date2)
print(t1,type(t1))
print(t2,type(t2))
print(date2,type(date2))
# 直接生成pandas的时刻数据→时间戳
# 数据类型为pandas的Timestamp
Timestamp是pandas中的数据结构,而datetime是datetime中的数据结构,这就是它们两者主要的区别。
2、pd.to_datetime
date1='2019-10-5 12:0:0'
date2=datetime.datetime(2020,1,1,15,0)
t1=pd.to_datetime(date1)
t2=pd.to_datetime(date2)
print(t1,type(t1))
print(t2,type(t2))
# pd.to_datetime():如果是单个时间数据,转换成pandas的时刻数据,数据类型为Timestamp
lst_date=['2020-12-12','2020-12-17','2020-12-23']
t3=pd.to_datetime(lst_date)
print(t3,type(t3))
# 多个时间数据,将会转换成pandas的DatatimeIndex
# pd.to_datetime →多个时间数据转换时间戳索引
from datetime import datetime
date1=[datetime(2020,1,1),datetime(2020,7,1),datetime(2020,9,1)]
date2=['2020-2-1','2020-5-1','2020-4-1','2020-6-1']
print(date1)
print(date2)
t1=pd.to_datetime(date2)
t2=pd.to_datetime(date2)
print(t1)
print(t2)
# 多个时间数据转换为DatetimeIndex
三、时间戳索引
1、Pandas时间戳索引:DatetimeIndex
核心技巧:pd.date_range(),DatetimeIndex是时间戳索引,而TimeSeries是时间序列。
# 时间序列
# DatetimeIndex TimeSeries
rng=pd.DatetimeIndex(['12/1/2020','12/2/2020','12/3/2020','12/4/2020'])
print(rng,type(rng))
print(rng[0],type(rng[0]))
print('\n')
# 直接生成时间戳索引,支持str,datetime.datetime
# 单个时间戳为Timestamp,多个时间戳为DatetimeIndex
st=pd.Series(np.random.rand(len(rng)),index=rng)
print(st,type(st))
print(st.index)
# 以DatetimeIndex为index的Series,为TimeSeries,时间序列
2、日期范围生成:pd.date_range()
# pd.date_range()-日期范围:生成日期范围
# 2种生成方式:1、start+end;2、start/end + periods
rng1=pd.date_range('1/1/2020','1/10/2020',normalize=True)
rng2=pd.date_range(start='1/1/2020',periods=10)
rng3=pd.date_range(end='1/30/2020 15:00:00',periods=10) # 增加了时、分、秒
print(rng1,type(rng1))
print(rng2)
print(rng3)
print('------')
# 直接生成DatetimeIndex
# pd.date_range(start=None,end=None,periods=None,freq='D',tz=None,normalize=False,name=None,closed=None,**kwargs)
# start:开始时间
# end:结束时间
# periods:偏移量
# freq:频率,默认天,pd.date_range()默认频率为日历日,pd.bdate_range()默认频率为工作日
# tz:时区
rng4=pd.date_range(start='1/1/2020 15:30',periods=10,name='xu peng',normalize=True)
print(rng4)
print("---------")
# normalize:时间参数值正则化到午夜时间戳(这里最后就直接变成0:00:00,并不是15:30:00)
# name:索引对象名称
3、日期范围:频率(1)
# pd.date_range()-日期范围:频率(1)
print(pd.date_range('2020/1/1','2020/1/4')) # 默认freq='D':每日历日
print(pd.date_range('2020/1/1','2020/1/4',freq='B')) # B:每工作日
print(pd.date_range('2020/1/1','2020/1/4',freq='H')) # H:每小时
print(pd.date_range('2020/1/1','2020/2/1',freq='W-MON'))
# W-MON:从指定星期几开始算起,每周
# 星期几缩写:MON/TUE/WED/THU/FRI/SAT/SUN
print(pd.date_range('2020/1/1','2020/5/1',freq='WOM-2MON'))
# WOM-2MON:每月的第几个星期几开始算,这里是每月第二个星期一
4、日期范围:频率(2)
# pd.date_range()-日期范围:频率(2)
print(pd.date_range('2019','2020',freq='M'))
print(pd.date_range('2019','2020',freq='Q-JAN'))
print(pd.date_range('2019','2020',freq='A-DEC'))
print('-------')
# M:每月最后一个日历日
# Q-月:指定月为季度末,每个季度末最后一月的最后一个日历日
# A-月:每年指定月份的最后一个日历日
# 月缩写:JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC
# 所以Q-月只有三种情况:1-4-7-10,2-5-8-11,3-6-9-12
print(pd.date_range('2019','2020',freq='BM'))
print(pd.date_range('2019','2020',freq='BQ-DEC'))
print(pd.date_range('2019','2020',freq='BA-DEC'))
print('------')
# BM:每月最后一个工作日
# BQ-月:指定月为季度末,每个季度末最后一月的最后一个工作日
# BA-月:每年指定月份的最后一个工作日
print(pd.date_range('2019','2020',freq='MS'))
print(pd.date_range('2019','2020',freq='QS-DEC'))
print(pd.date_range('2019','2020',freq='AS-DEC'))
print('-------')
# M:每月第一个日历日
# Q-月:指定月为季度末,每个季度末最后一月的第一个日历日
# A-月:每年指定月份的第一个日历日
print(pd.date_range('2019','2020',freq='BMS'))
print(pd.date_range('2019','2020',freq='BQS-DEC'))
print(pd.date_range('2019','2020',freq='BAS-DEC'))
print('-------')
# BM:每月第一个工作日
# BQ-月:指定月为季度末,每个季度末最后一月的第一个工作日
# BA-月:每年指定月份的第一个工作日
5、日期范围:频率(3)——符合频率
# pd.date_range()-日期范围:复合频率
print(pd.date_range('2019/5/1','2020/2/1',freq='7D')) # 7天
print(pd.date_range('2019/5/1','2020/1/1',freq='5h30min')) # 2小时30分钟
print(pd.date_range('2019','2020',freq='2MS')) # 2月,每月最后一个日历日