代码语言:javascript复制
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
代码语言:javascript复制# 引入 创建时间序列
from datetime import datetime
t1 = datetime(2009, 10, 20)
t1
代码语言:javascript复制datetime.datetime(2009, 10, 20, 0, 0)
代码语言:javascript复制# 列表创建时间序列
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
代码语言:javascript复制[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)]
代码语言:javascript复制# 将时间变为索引
s1 = Series(np.random.rand(6), index=date_list)
s1
代码语言:javascript复制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
代码语言:javascript复制s1.values
代码语言:javascript复制array([0.97129488, 0.08596965, 0.11768269, 0.89122463, 0.92245809,
0.34687534])
代码语言:javascript复制s1.index
代码语言:javascript复制DatetimeIndex(['2016-02-04', '2016-09-04', '2016-10-04', '2017-07-04',
'2017-09-04', '2017-10-04'],
dtype='datetime64[ns]', freq=None)
代码语言:javascript复制# 通过index位置 返回数据值
s1[1]
代码语言:javascript复制0.08596964757361214
代码语言:javascript复制# 通过datetime 数据类型 返回信息
s1[datetime(2016,2,4)]
代码语言:javascript复制0.9712948753332638
代码语言:javascript复制s1['2016-2-4']
代码语言:javascript复制0.9712948753332638
代码语言:javascript复制s1['20160204']
代码语言:javascript复制0.9712948753332638
代码语言:javascript复制s1['2016-02']
代码语言:javascript复制2016-02-04 0.971295
dtype: float64
代码语言:javascript复制s1['2016']
代码语言:javascript复制2016-02-04 0.971295
2016-09-04 0.085970
2016-10-04 0.117683
dtype: float64
代码语言:javascript复制# 能够产生一段时间以内的datetime的方法(起止 间隔 步长默认d天W。周日开始周一结束,W-MON变为正常周)
date_list_new = pd.date_range('2016-02-04',periods=100, freq='W-MON')
date_list_new
代码语言:javascript复制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')
代码语言:javascript复制s2 = Series(np.random.rand(100),index=date_list_new)
s2
代码语言:javascript复制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