时间序列


  • 时间戳(timestamp)
  • 固定周期(period)
  • 时间间隔(interval)

date_range


  • 可以指定开始时间与周期
  • H:小时
  • D:天
  • M:月

import pandas as pd
import numpy as np
#TIMES #2019 Jul 1 #7/7/2019 1/7/2016 2019-7-01 2019/07/01
rng=pd.date_range('2019-07-01',periods=10,freq='D')#可以指定开始时间与周期:H:小时,D:天,M:月,前面可以加数字,如‘3D’
rng

pandas时间序列处理_Date&Time

time=pd.Series(np.random.randn(20),
index=pd.date_range(pd.datetime(2019,1,1),periods=20))
print(time)

pandas时间序列处理_数据_02

#truncate过滤
time.truncate(before='2019-01-10')#2019-1-10之后的数据,包括2019-01-10

pandas时间序列处理_python_03

time.truncate(after='2019-1-10')#由上可知

pandas时间序列处理_python_04

print(time['2019-01-15'])

0.22996364154291105

data=pd.date_range('2010-01-01','2011-01-01',freq='M')
print(data)

pandas时间序列处理_python_05

pandas时间序列处理_数据_06

时间戳


pd.Timestamp('2019-01-10')
#out : imestamp('2019-01-10 00:00:00')

# 可以指定更多细节
pd.Timestamp('2016-07-10 10')
pd.Timestamp('2016-07-10 13:14')
pd.Timestamp('2016-07-10 13:14:55')

时间戳


pd.Period('2019-01')
pd.Period('2016-01-01')

pandas时间序列处理_时间戳_07

时间偏移量

pd.Timedelta('1 day')
print(pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day'))
print(pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('1 day'))
print(pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('15 ns'))

pandas时间序列处理_Date&Time_08

p1 = pd.period_range('2016-01-01 10:10', freq = '25H', periods = 10)
p1

pandas时间序列处理_Date&Time_09

p2 = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)#一天一个小时
p2

pandas时间序列处理_python_10

指定索引

rng = pd.date_range('2019 Jul 1', periods = 10, freq = 'D')

pd.Series(range(len(rng)), index = rng)

pandas时间序列处理_时间戳_11

#根据时间点获取数据
periods = [pd.Period('2019-01'), pd.Period('2019-02'), pd.Period('2019-03')]
ts = pd.Series(np.random.randn(len(periods)), index = periods)
ts

pandas时间序列处理_Date&Time_12

时间戳和时间周期可以转换

ts = pd.Series(range(10), pd.date_range('07-10-16 8:00', periods = 10, freq = 'H'))
ts

pandas时间序列处理_Date&Time_13

ts_period = ts.to_period()
ts_period

pandas时间序列处理_Date&Time_14

pandas时间序列处理_数据_15