【数据分析可视化】时间序列操作基础

网友投稿 316 2022-11-23

【数据分析可视化】时间序列操作基础

import numpy as np import pandas as pd from pandas import Series,DataFrame # 引入 创建时间序列 from datetime import datetime t1 = datetime(2009, 10, 20) t1 datetime.datetime(2009, 10, 20, 0, 0) # 列表创建时间序列 date_list = [ datetime(2016,2,4), datetime(2016,9,4), datetime(2016,10,4), datetime(2017,7,4), datetime(2017,9,4), datetime(2017,10,4) ] date_list [datetime.datetime(2016, 2, 4, 0, 0), datetime.datetime(2016, 9, 4, 0, 0), datetime.datetime(2016, 10, 4, 0, 0), datetime.datetime(2017, 7, 4, 0, 0), datetime.datetime(2017, 9, 4, 0, 0), datetime.datetime(2017, 10, 4, 0, 0)] # 将时间变为索引 s1 = Series(np.random.rand(6), index=date_list) s1 2016-02-04 0.971295 2016-09-04 0.085970 2016-10-04 0.117683 2017-07-04 0.891225 2017-09-04 0.922458 2017-10-04 0.346875 dtype: float64 s1.values array([0.97129488, 0.08596965, 0.11768269, 0.89122463, 0.92245809, 0.34687534]) s1.index DatetimeIndex(['2016-02-04', '2016-09-04', '2016-10-04', '2017-07-04', '2017-09-04', '2017-10-04'], dtype='datetime64[ns]', freq=None) # 通过index位置 返回数据值 s1[1] 0.08596964757361214 # 通过datetime 数据类型 返回信息 s1[datetime(2016,2,4)] 0.9712948753332638 s1['2016-2-4'] 0.9712948753332638 s1['20160204'] 0.9712948753332638 s1['2016-02'] 2016-02-04 0.971295 dtype: float64 s1['2016'] 2016-02-04 0.971295 2016-09-04 0.085970 2016-10-04 0.117683 dtype: float64 # 能够产生一段时间以内的datetime的方法(起止 间隔 步长默认d天W。周日开始周一结束,W-MON变为正常周) date_list_new = pd.date_range('2016-02-04',periods=100, freq='W-MON') date_list_new DatetimeIndex(['2016-02-08', '2016-02-15', '2016-02-22', '2016-02-29', '2016-03-07', '2016-03-14', '2016-03-21', '2016-03-28', '2016-04-04', '2016-04-11', '2016-04-18', '2016-04-25', '2016-05-02', '2016-05-09', '2016-05-16', '2016-05-23', '2016-05-30', '2016-06-06', '2016-06-13', '2016-06-20', '2016-06-27', '2016-07-04', '2016-07-11', '2016-07-18', '2016-07-25', '2016-08-01', '2016-08-08', '2016-08-15', '2016-08-22', '2016-08-29', '2016-09-05', '2016-09-12', '2016-09-19', '2016-09-26', '2016-10-03', '2016-10-10', '2016-10-17', '2016-10-24', '2016-10-31', '2016-11-07', '2016-11-14', '2016-11-21', '2016-11-28', '2016-12-05', '2016-12-12', '2016-12-19', '2016-12-26', '2017-01-02', '2017-01-09', '2017-01-16', '2017-01-23', '2017-01-30', '2017-02-06', '2017-02-13', '2017-02-20', '2017-02-27', '2017-03-06', '2017-03-13', '2017-03-20', '2017-03-27', '2017-04-03', '2017-04-10', '2017-04-17', '2017-04-24', '2017-05-01', '2017-05-08', '2017-05-15', '2017-05-22', '2017-05-29', '2017-06-05', '2017-06-12', '2017-06-19', '2017-06-26', '2017-07-03', '2017-07-10', '2017-07-17', '2017-07-24', '2017-07-31', '2017-08-07', '2017-08-14', '2017-08-21', '2017-08-28', '2017-09-04', '2017-09-11', '2017-09-18', '2017-09-25', '2017-10-02', '2017-10-09', '2017-10-16', '2017-10-23', '2017-10-30', '2017-11-06', '2017-11-13', '2017-11-20', '2017-11-27', '2017-12-04', '2017-12-11', '2017-12-18', '2017-12-25', '2018-01-01'], dtype='datetime64[ns]', freq='W-MON') s2 = Series(np.random.rand(100),index=date_list_new) s2 2016-02-08 0.828582 2016-02-15 0.348196 2016-02-22 0.012437 2016-02-29 0.148311 2016-03-07 0.021719 ... 2017-12-04 0.345226 2017-12-11 0.945203 2017-12-18 0.069342 2017-12-25 0.134574 2018-01-01 0.975369 Freq: W-MON, Length: 100, dtype: float64

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 实战练手项目之酒店管理系统的实现流程
下一篇:【数据分析可视化】通过apply进行数据预处理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~