Python--DateTime.s(时刻数据TimeStamp丶时间戳索引、时期对象Period)

Pandas  时刻数据 / 时间戳索引 / 时期对象


 时刻数据   TimeStamp 👇

 时刻数据 代表时间点,是Python的数据类型 pandas.Timestamp  是一个时间戳

import pandas as pd
import datetime as dt

Timestamp创建 , 时间戳的实例化

date1 = dt.datetime.now()  
date2 = '2019-10-10 11:30:30'
t1 = pd.Timestamp(date1)
t2 = pd.Timestamp(date2)
print(t1,type(t1))
print(t2,type(t2))
#------输出-----#
2019-11-05 23:46:38.328008 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2019-10-10 11:30:30 <class 'pandas._libs.tslibs.timestamps.Timestamp'>

 👆 直接实例化。

to_datetime(time)

👇 to_datetime() 作用和上面的一样,属于调用方法实例化, 生成一个Timestamp对象 .

t3 = pd.to_datetime('2019-10-10')
t3

#------输出-----#
Timestamp('2019-10-10 00:00:00')

👇 如何一次生成多个,可以选择用 List (列表) 的方式 :

dates = ['2019-10-10','2019-10-11','2019-10-12','2019-10-13','2019-10-14','2019-10-15','2019-10-16']
t3 = pd.to_datetime(dates)

#-----输出-----#
DatetimeIndex(['2019-10-10', '2019-10-11', '2019-10-12', '2019-10-13','2019-10-14', '2019-10-15', '2019-10-16'],dtype='datetime64[ns]', freq=None)

👆 如果是多时间数据,生成的是一个DateTimeIndex ,是一个时间戳的索引,也就是时间索引。时间类型的索引 。

👉 如果生成多个时间数据,列表中夹杂了非 Time 格式的数据 ,默认会报错 ,它的解决方法就是添加 errors 参数

👉 errors = 'ignore' 会忽略错误,对原数据不可解析 ,返回原始数据,生成的是一个一般的 Index 索引 。 

dates = ['2019-10-10','2019-10-11','2019-10-12','abc']
t3 = pd.to_datetime(dates,errors='ignore')

#-----输出-----#
Index(['2019-10-10', '2019-10-11', '2019-10-12', 'abc'], dtype='object')

👉 errors = 'coerce' :非Time数据不可解析时,返回Nat值(not a time),生成的对象还是一个 DateTimeIndex

dates = ['2019-10-10','2019-10-11','2019-10-12','abc']
t3 = pd.to_datetime(dates,errors='coerce')

#-----输出-----#
DatetimeIndex(['2019-10-10', '2019-10-11', '2019-10-12', 'NaT'], dtype='datetime64[ns]', freq=None)

 


 

时间戳索引   DateTimeIndex 👇

 

# DateTimeIndex 和TimeSeries 时间序列
dts = pd.DatetimeIndex(['2019-10-10','2019-10-11','2019-10-12','2019-10-13'])

#-----输出-----#
DatetimeIndex(['2019-10-10', '2019-10-11', '2019-10-12', '2019-10-13'], dtype='datetime64[ns]', freq=None)

👆  直接 生成|实例化  一个 时间序列 DateTimeIndex   ,支持类型包括 : str ,datetime.datetime

times = pd.Series(np.random.rand(len(dts)),index=dts)

#-----输出----#
2019-10-10    0.185203
2019-10-11    0.925192
2019-10-12    0.103523
2019-10-13    0.734690
dtype: float64

👆  时间序列,以 DateTimeIndexindex 的Series (以时间戳为索引的Series)

👇 pd.date_range()  - 时间范围 , 生成时间范围列表 ,参数start ,end :

dg = pd.date_range('2019-10-01','2019-10-10')  #默认按 '天' 生成
dg1 = pd.date_range(start=dt.datetime.now(),periods=10)  #periods  生成时间个数,从开始时间算
dg2 = pd.date_range('2019-10-01','2019-10-05',periods=10) #时长不够 则按h划分
DatetimeIndex(['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04',
               '2019-10-05', '2019-10-06', '2019-10-07', '2019-10-08',
               '2019-10-09', '2019-10-10'],
              dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2019-11-06 02:30:23.148685', '2019-11-07 02:30:23.148685',
               '2019-11-08 02:30:23.148685', '2019-11-09 02:30:23.148685',
               '2019-11-10 02:30:23.148685', '2019-11-11 02:30:23.148685',
               '2019-11-12 02:30:23.148685', '2019-11-13 02:30:23.148685',
               '2019-11-14 02:30:23.148685', '2019-11-15 02:30:23.148685'],
              dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2019-10-01 00:00:00', '2019-10-01 10:40:00',
               '2019-10-01 21:20:00', '2019-10-02 08:00:00',
               '2019-10-02 18:40:00', '2019-10-03 05:20:00',
               '2019-10-03 16:00:00', '2019-10-04 02:40:00',
               '2019-10-04 13:20:00', '2019-10-05 00:00:00'],
              dtype='datetime64[ns]', freq=None)

date_range()  的一些参数说明 :

# start : 开始时间
# end : 结束时间
# periods : 生成时间数量 
# freq : 频率   按频率生成  设置为s 为秒 ,h ,d ,m , y
# normalize : 转换成午夜时间    设置为True 时,默认去除 时分秒
# closed : 时期区间的闭合   closed ='left'  左闭合  默认为None  全闭

时间频率 timeseries.offset_aliases -- freq 参数 :

# 常见常用参数 :
# freq   默认为"D"  每天
# B 每个工作日
# H 每小时
# M 每个月的最后一天
# T T/Min  每分钟
# Q-DEC  指定某月为季度末,每个季度的最后一个月的最后一日
# S 每秒钟
# L 每毫秒 (千分之一秒)
# U 每微秒 (百万分之一秒)
print(pd.date_range('2019/1/1','2019/2/1',freq='W-Mon'))

#-----输出-----#
DatetimeIndex(['2019-01-07', '2019-01-14', '2019-01-21', '2019-01-28'], dtype='datetime64[ns]', freq='W-MON')

👆 W-MON :从指定星期几开始算起,每周的星期几
👆 星期几缩写 : mon / tue / wed / thu / fri / sat / sun

print(pd.date_range('2019/1/1','2019/5/1',freq='WOM-2MON'))

#-----输出-----#
DatetimeIndex(['2019-01-14', '2019-02-11', '2019-03-11', '2019-04-08'], dtype='datetime64[ns]', freq='WOM-2MON')

👆  WON-2MON ,每月的第几个星期几开始算,这里是每月第二个星期一

 

复合频率 话不多说,直接看例子 :

ad = pd.date_range('2019-10-10','2019-10-31',freq='7D')   # 每隔7天生成一个

#------输出-----#
DatetimeIndex(['2019-10-10', '2019-10-17', '2019-10-24', '2019-10-31'], dtype='datetime64[ns]', freq='7D')

ad = pd.date_range('2019-10-10','2019-10-11',freq='2H30MIN')   #每隔2h30min生成一个 

#-----输出-----#
DatetimeIndex(['2019-10-10 00:00:00', '2019-10-10 02:30:00',
               '2019-10-10 05:00:00', '2019-10-10 07:30:00',
               '2019-10-10 10:00:00', '2019-10-10 12:30:00',
               '2019-10-10 15:00:00', '2019-10-10 17:30:00',
               '2019-10-10 20:00:00', '2019-10-10 22:30:00'],
              dtype='datetime64[ns]', freq='150T')

时刻频率的改变 : 例如 :

ts = pd.Series(np.random.rand(5),index=pd.date_range('2019-1-1',periods=5,freq='H'))

#-----输出-----#
2019-01-01 00:00:00    0.235263
2019-01-01 01:00:00    0.116529
2019-01-01 02:00:00    0.475352
2019-01-01 03:00:00    0.285782
2019-01-01 04:00:00    0.278366
Freq: H, dtype: float64

👆 如何把 上方的频率 H 降频 为min  , s 或者其他呢? 用   .asfreq()  方法

ts.asfreq('2H')

#-----输出-----#
2019-01-01 00:00:00    235.263032
2019-01-01 02:00:00    475.352441
2019-01-01 04:00:00    278.366048
Freq: 2H, dtype: float64

ts.asfreq('30min') 
#-----输出-----#
2019-01-01 00:00:00    235.263032
2019-01-01 00:30:00           NaN
2019-01-01 01:00:00    116.529416
2019-01-01 01:30:00           NaN
2019-01-01 02:00:00    475.352441
2019-01-01 02:30:00           NaN
2019-01-01 03:00:00    285.781654
2019-01-01 03:30:00           NaN
2019-01-01 04:00:00    278.366048
Freq: 30T, dtype: float64

 👆  当超频时,默认为NaN,通过 method = 'ffill' 时 向前填充 , bfill 时向后填充.

 

超前 和 滞后 :数据值相对索引自定义向前后移动 :  .shift()

 

ts.shift(1)

#-----输出-----#
2019-01-01 00:00:00         NaN
2019-01-01 01:00:00    0.517237
2019-01-01 02:00:00    0.152740
2019-01-01 03:00:00    0.790932
2019-01-01 04:00:00    0.988369
Freq: H, dtype: float64

 

👆 : 值为正数 值往后面挪动 , 值为负数 值往前挪动 .也可以理解为时间移动了,其实是值移动了.

👆 : 有个很强大的功能,例如 : 计算当前值和上一次值的变化百分比

ts = ts * 1000
print(ts/ts.shift(1)-1)

#-----输出-----#
2019-01-01 00:00:00         NaN
2019-01-01 01:00:00   -0.504685
2019-01-01 02:00:00    3.079248
2019-01-01 03:00:00   -0.398800
2019-01-01 04:00:00   -0.025949
Freq: H, dtype: float64

## 解释 : 
具体的意思就是第二天相比比第一天相比增长还是下降  ,每天的增降再Sum一下便能得到整个月的数值与上月比较涨幅 。 (今天的值除以昨天的值的再减百分之百)

 

移动时间  .shift() +freq :

print(ts)
ts.shift(-30,freq="T")

#-----输出-----#
2019-01-01 00:00:00    59.981144
2019-01-01 01:00:00    39.215781
2019-01-01 02:00:00    62.647637
2019-01-01 03:00:00    57.058369
2019-01-01 04:00:00    84.421470
Freq: H, dtype: float64

2018-12-31 23:30:00    59.981144
2019-01-01 00:30:00    39.215781
2019-01-01 01:30:00    62.647637
2019-01-01 02:30:00    57.058369
2019-01-01 03:30:00    84.421470
Freq: H, dtype: float64
View Code

👆  加上参数 freq 代表移动时间索引  整体移动,  #不添加freq 值移动,添加freq 时间移动#


时期对象    Period 👇

 创建一个 Period 对象

pd.Period('2019',freq='2M')

#-----输出-----#
Period('2019-01', '2M')

👆 这是一个以2019-01开始,月为频率的时间构造器

👆 pd.Period参数 : freq 👉 指明该 period 的长度 ,时间戳说明时间的具体位置

 时期~范围 pd.period_range() :

pd.period_range('2019-1-1','2019-10-1',freq='M')

#-----输出-----#
PeriodIndex(['2019-01', '2019-02', '2019-03', '2019-04', '2019-05', '2019-06',
             '2019-07', '2019-08', '2019-09', '2019-10'],
            dtype='period[M]', freq='M')

👆 返回的是一个periodIndex 对象 : 时期索引对象

pd.Series(np.random.rand(10),index=perd)

#-----输出-----#
2019-01    0.512124
2019-02    0.289445
2019-03    0.337499
2019-04    0.828296
2019-05    0.574218
2019-06    0.290252
2019-07    0.806585
2019-08    0.233860
2019-09    0.833617
2019-10    0.143754
Freq: M, dtype: float64

  Timestamp 表示一个时间戳 ,表示一个具体的时间

  Period 表示一个时期 ,一个时间段。   作为索引来说,区别不大!

# 频率的转换 ,下面展示的是  由M  转 D :

perd.asfreq('D',how='S')

#-----输出-----#
PeriodIndex(['2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01',
             '2019-05-01', '2019-06-01', '2019-07-01', '2019-08-01',
             '2019-09-01', '2019-10-01'],
            dtype='period[D]', freq='D')

how =' S'  指定第一个值   S 代表 开始 Start ,E 代表末尾 End


 

时间戳时期之间 的转换 , pd.to_period , pd.to_timestamp

pt = pd.period_range('2018','2019',freq='M')
st = pd.date_range('2019/1/1',periods=10,freq='MS')

pts = pd.Series(np.random.rand(len(pt)),index=pt)
pts.to_timestamp   #每月的最后一日,转换成每日

sts = pd.Series(np.random.rand(len(st)),index=st)
sts.to_period    #每月,转换为每月的第一天
<bound method Series.to_timestamp of 2018-01    0.910903
2018-02    0.582622
2018-03    0.942149
2018-04    0.849428
2018-05    0.866768
2018-06    0.840774
2018-07    0.085061
2018-08    0.963129
2018-09    0.256044
2018-10    0.727409
2018-11    0.043810
2018-12    0.544194
2019-01    0.030864
Freq: M, dtype: float64>

<bound method Series.to_period of 2019-01-01    0.699750
2019-02-01    0.685037
2019-03-01    0.218881
2019-04-01    0.811947
2019-05-01    0.102095
2019-06-01    0.869153
2019-07-01    0.654644
2019-08-01    0.792193
2019-09-01    0.179387
2019-10-01    0.855273
Freq: MS, dtype: float64>
View Code

 

 

 

 

posted @ 2019-11-07 02:38  逍遥大帝  阅读(6750)  评论(0编辑  收藏  举报