Pandas的时间序列

  1. import time 和 import datatime 导入时间的库,time.time() 指现在此时此刻的时间戳~,
    1594282465.6077569指多少秒的意思。
    Pandas 基础 - 时间及函数

  2. pandas 中有个库叫timestamp,能将s 转化为日期,unit = “s”,如果写unit = "m"或者"h"会报错。
    Pandas 基础 - 时间及函数

  3. pd 中能转化为timestamp 的四种形式:2020-12-12,2020/12/12,2020-12-12,第四种,相当于excel 中的date 函数:指定年,月,日。
    Pandas 基础 - 时间及函数
    Pandas 基础 - 时间及函数

  4. 转化为日期时间,pd.to_datetime(pd.Series([2019-12-10])) 会把属于文本中的2019/12/10转化日期格式。pd.to_datetime ,这跟sql语句中的date(“20191210”) 很相似。
    Pandas 基础 - 时间及函数

  5. pd.to_datetime(“2019#12#12”, format = “%Y%m%d”) ,转化为日期的中间可以用*,/,+ ,# 来表示,但是format 的第一个%"Y"一定要是大写,剩下的小写。

Pandas 基础 - 时间及函数

  1. pd.date_range(“2020-12-10”,“2020-12-12”,freq = "D’)。
    Pandas 基础 - 时间及函数

DataFrame 的日期转化

  1. raw_data.日期 是日期格式,而astype(str) 转化为字符串 格式。
    Pandas 基础 - 时间及函数

  2. data 是字符串格式,而pd.to_datatime( ) 转化为日期 格式。
    Pandas 基础 - 时间及函数

  3. raw_data.日期 是日期格式,转化为其他想要的日期格式 为dt.trftime("%Y%m%d") 或者 dt.strftime(%Y)。
    Pandas 基础 - 时间及函数

  4. 第二种转化为年月日的格式,dt.year = dt.strftime(%Y) 前者是int 整数,后者是文本。
    Pandas 基础 - 时间及函数

  5. raw_data.日期 为日期格式,dt.date 表示转化为日期文本的类型 ,而dt. time 转化为时间的文本。
    Pandas 基础 - 时间及函数
    Pandas 基础 - 时间及函数

DataFrame 的日期切片与索引

  1. import copy板块 , 其中copy 有 copy() 与deepcopy() 两种函数。列表和字典,直接用a.copy 。
    Pandas 基础 - 时间及函数

  2. 用日期设置新的索引,将data.index = data.date ,原数据删除date 那一列。
    Pandas 基础 - 时间及函数

  3. 用日期的索引,即行索引去切片,常用的日期换索引。
    Pandas 基础 - 时间及函数

  4. date.sort_index() 从小到大排序 ,里面可以放ascending = False 进行倒叙排序。
    Pandas 基础 - 时间及函数

  5. 图中的1 =data.day of week表示一周中的星期几,因为国外的周日为第一天,所以我们要+1,2 = dayofyear 表示一年中的第多少天,3 = weekofyear 表示一年中的第几周。
    Pandas 基础 - 时间及函数

  6. 判断日期那天是否是一个月中的开始的月份,即是不是10.1,11.1…。
    Pandas 基础 - 时间及函数
    附件:更多详细的请查看链接:日期与时间汇总

DateOffset 的对象

  1. Dateoffest 与excel 中的offest 差不多,都是定位。前者要用pd 转为为timestamp 格式,然后再进行year,month, days 的偏移,而excel 中要用 行和列偏移几个来确定,而sql 中有个date_add(“2020-12-1”,interval n day) = 2020-12-2 和date_sub(“2020-12-1”,interval 1 year) =2019-12-1。
    Pandas 基础 - 时间及函数

DataFrame 时间的移动/滞后、频率转换、采样

ts.shfit() 时间的移动

  1. 设置时间,index = pd.date_range(“2020-11-12”,periods = 4 ,freq =“W”) 表示从2020-11-12 开始range 4 个日期,按照隔周range 即 7 天。Series 后面要带index= index 表示ts 的index 是日期,不写的话默认从0-3。
    Pandas 基础 - 时间及函数
  2. 时间的上移及下移,ts.shift( 1 ) 移的不是索引,注意是数据向下上移 。
    Pandas 基础 - 时间及函数

df.asfreq() 频率的转换

  1. pd.asfreq() 中的freq 参数可以是W-周,S-秒,D-日
    Pandas 基础 - 时间及函数
    哈哈,每天更新一点,没准我再也不是菜鸡,纯手打!!!!
    查看更多请点击此链接,反正不要钱!

相关文章: