微信公众号:数据皮皮侠
如果你觉得该公众号对你有帮助,欢迎关注、推广宣传
内容目录:时间序列Python实现
时间序列 简介1.3 时间戳1.4 时间区间1.5 时间加减1.6 指定索引1.7 时间戳和时间周期可以转换
时间序列 简介
1.3 时间戳
1import pandas as pd
2pd.Timestamp('2016-07-10')
Timestamp('2016-07-10 00:00:00')
可以指定更多细节
1pd.Timestamp('2020-04-11 10')
Timestamp('2020-04-11 10:00:00')
1pd.Timestamp('2020-04-11 10:15')
Timestamp('2020-04-11 10:15:00')
1.4 时间区间
2020年1月1号::
1pd.Period('2020-01-01')
Period('2020-01-01', 'D')
2020年的一月份:
1pd.Period('2020-01')
Period('2020-01', 'M')
1.5 时间加减
TIME OFFSETS
产生一个一天的时间偏移量:
1pd.Timedelta('1 day')
Timedelta('1 days 00:00:00')
得到2020-01-01 10:10的后一天时刻:
1pd.Period('2020-01-01 10:10') + pd.Timedelta('1 day')
Period('2020-01-02 10:10', 'T')
时间戳加减:
1pd.Timestamp('2020-01-01 10:10') + pd.Timedelta('1 day')
Timestamp('2020-01-02 10:10:00')
加15 ns:
1pd.Timestamp('2020-01-01 10:10') + pd.Timedelta('30 ns')
Timestamp('2020-01-01 10:10:00.000000030')
在时间间隔刹参数中,我们既可以写成25H,也可以写成1D1H这种通俗的表达:
1p1 = pd.period_range('2020-01-01 10:10', freq = '25H', periods = 10)
2p2 = pd.period_range('2020-01-01 10:10', freq = '1D1H', periods = 10)
1p1
2PeriodIndex(['2020-01-01 10:00', '2020-01-02 11:00', '2020-01-03 12:00',
3 '2020-01-04 13:00', '2020-01-05 14:00', '2020-01-06 15:00',
4 '2020-01-07 16:00', '2020-01-08 17:00', '2020-01-09 18:00',
5 '2020-01-10 19:00'],
6 dtype='period[25H]', freq='25H')
7p2
8PeriodIndex(['2020-01-01 10:00', '2020-01-02 11:00', '2020-01-03 12:00',
9 '2020-01-04 13:00', '2020-01-05 14:00', '2020-01-06 15:00',
10 '2020-01-07 16:00', '2020-01-08 17:00', '2020-01-09 18:00',
11 '2020-01-10 19:00'],
12 dtype='period[25H]', freq='25H')
1.6 指定索引
1rng = pd.date_range('2019 Jul 1', periods = 10, freq = 'D')
2rng
3pd.Series(range(len(rng)), index = rng)
显示结果:
12019-07-01 0
22019-07-02 1
32019-07-03 2
42019-07-04 3
52019-07-05 4
62019-07-06 5
72019-07-07 6
82019-07-08 7
92019-07-09 8
102019-07-10 9
11Freq: D, dtype: int64
构造任意的Series结构时间序列数据:
1import numpy as np
2import pandas as pd
3periods = [pd.Period('2019-01'), pd.Period('2019-02'), pd.Period('2019-03')]
4ts = pd.Series(np.random.randn(len(periods)), index = periods)
5
6ts
72019-01 -3.101546
82019-02 -1.911547
92019-03 1.160135
10Freq: M, dtype: float64
11
12type(ts.index)
13pandas.core.indexes.period.PeriodIndex
1.7 时间戳和时间周期可以转换
产生时间周期:
1ts = pd.Series(range(10), pd.date_range('07-10-19 8:00', periods = 10, freq = 'H'))
2ts
32019-07-10 08:00:00 0
42019-07-10 09:00:00 1
52019-07-10 10:00:00 2
62019-07-10 11:00:00 3
72019-07-10 12:00:00 4
82019-07-10 13:00:00 5
92019-07-10 14:00:00 6
102019-07-10 15:00:00 7
112019-07-10 16:00:00 8
122019-07-10 17:00:00 9
13Freq: H, dtype: int64
将时间周期转化为时间戳:
1ts_period = ts.to_period()
2ts_period
32019-07-10 08:00:00 0
42019-07-10 09:00:00 1
52019-07-10 10:00:00 2
62019-07-10 11:00:00 3
72019-07-10 12:00:00 4
82019-07-10 13:00:00 5
92019-07-10 14:00:00 6
102019-07-10 15:00:00 7
112019-07-10 16:00:00 8
122019-07-10 17:00:00 9
13Freq: H, dtype: int64
时间周期和时间戳区别:
对时间周期的切片操作:
1ts_period['2019-07-10 08:30':'2019-07-10 11:45']
22019-07-10 08:00 0
32019-07-10 09:00 1
42019-07-10 10:00 2
52019-07-10 11:00 3
6Freq: H, dtype: int64
对时间戳的切片操作结果:
1ts['2019-07-10 08:30':'2019-07-10 11:45']
22019-07-10 09:00:00 1
32019-07-10 10:00:00 2
42019-07-10 11:00:00 3
5Freq: H, dtype: int64


