pandas时间序列分析的基本操作方法
文章目录
- 导入需要的库
- 时间序列
- 生成时间序列
- truncate过滤
- 时间戳
- 时间区间
- 指定索引
- 时间戳和时间周期可以转换
- 数据重采样
- 插值方法
导入需要的库
代码语言:javascript复制import pandas as pd
import numpy as np
import datetime as dt
时间序列
- 时间戳(timestamp)
- 固定周期(period)
- 时间间隔(interval)
生成时间序列
- 可以指定开始时间与周期
- H:小时
- D:天
- M:月
# TIMES #2016 Jul 1 7/1/2016 1/7/2016 2016-07-01 2016/07/01
rng = pd.date_range('2016-07-01', periods = 10, freq = '3D')
rng
代码语言:javascript复制DatetimeIndex(['2016-07-01', '2016-07-04', '2016-07-07', '2016-07-10',
'2016-07-13', '2016-07-16', '2016-07-19', '2016-07-22',
'2016-07-25', '2016-07-28'],
dtype='datetime64[ns]', freq='3D')
代码语言:javascript复制time=pd.Series(np.random.randn(20),
index=pd.date_range(dt.datetime(2016,1,1),periods=20))
print(time)
代码语言:javascript复制2016-01-01 -0.067209
2016-01-02 0.480689
2016-01-03 -0.152052
2016-01-04 0.077139
2016-01-05 -1.775043
2016-01-06 -1.184273
Freq: D, dtype: float64
truncate过滤
代码语言:javascript复制time.truncate(before='2016-1-10')
代码语言:javascript复制2016-01-10 -0.349605
2016-01-11 2.159193
2016-01-12 0.077578
2016-01-13 0.084981
2016-01-14 -0.099995
2016-01-15 -1.327124
2016-01-16 1.352626
Freq: D, dtype: float64
代码语言:javascript复制time.truncate(after='2016-1-10')
代码语言:javascript复制2016-01-01 -0.067209
2016-01-02 0.480689
2016-01-03 -0.152052
2016-01-04 0.077139
2016-01-05 -1.775043
2016-01-06 -1.184273
2016-01-07 -1.247371
2016-01-08 -0.686737
2016-01-09 -1.787544
2016-01-10 -0.349605
Freq: D, dtype: float64
代码语言:javascript复制print(time['2016-01-15'])
代码语言:javascript复制-1.3271240245020821
代码语言:javascript复制print(time['2016-01-15':'2016-01-20'])
代码语言:javascript复制2016-01-15 -1.327124
2016-01-16 1.352626
2016-01-17 -0.075599
2016-01-18 1.026780
2016-01-19 -0.286614
2016-01-20 -0.017546
Freq: D, dtype: float64
代码语言:javascript复制data=pd.date_range('2010-01-01','2011-01-01',freq='M')
print(data)
代码语言:javascript复制DatetimeIndex(['2010-01-31', '2010-02-28', '2010-03-31', '2010-04-30',
'2010-05-31', '2010-06-30', '2010-07-31', '2010-08-31',
'2010-09-30', '2010-10-31', '2010-11-30', '2010-12-31'],
dtype='datetime64[ns]', freq='M')
常见的格式
时间戳
代码语言:javascript复制pd.Timestamp('2016-07-10')
代码语言:javascript复制Timestamp('2016-07-10 00:00:00')
代码语言:javascript复制# 可以指定更多细节
pd.Timestamp('2016-07-10 10')
代码语言:javascript复制Timestamp('2016-07-10 10:00:00')
代码语言:javascript复制pd.Timestamp('2016-07-10 10:15')
代码语言:javascript复制Timestamp('2016-07-10 10:15:00')
代码语言:javascript复制# How much detail can you add?
代码语言:javascript复制t = pd.Timestamp('2016-07-10 10:15')
时间区间
代码语言:javascript复制pd.Period('2016-01')
代码语言:javascript复制Period('2016-01', 'M')
代码语言:javascript复制pd.Period('2016-01-01')
代码语言:javascript复制Period('2016-01-01', 'D')
代码语言:javascript复制# TIME OFFSETS
pd.Timedelta('1 day')
代码语言:javascript复制Timedelta('1 days 00:00:00')
代码语言:javascript复制pd.Period('2016-01-01 10:10') pd.Timedelta('1 day')
代码语言:javascript复制Period('2016-01-02 10:10', 'T')
代码语言:javascript复制pd.Timestamp('2016-01-01 10:10') pd.Timedelta('1 day')
代码语言:javascript复制Timestamp('2016-01-02 10:10:00')
代码语言:javascript复制pd.Timestamp('2016-01-01 10:10') pd.Timedelta('15 ns')
代码语言:javascript复制Timestamp('2016-01-01 10:10:00.000000015')
代码语言:javascript复制p1 = pd.period_range('2016-01-01 10:10', freq = '25H', periods = 10)
代码语言:javascript复制p2 = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)
代码语言:javascript复制p1
代码语言:javascript复制PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',
'2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',
'2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',
'2016-01-10 19:00'],
dtype='period[25H]', freq='25H')
代码语言:javascript复制p2
代码语言:javascript复制PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',
'2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',
'2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',
'2016-01-10 19:00'],
dtype='period[25H]', freq='25H')
指定索引
代码语言:javascript复制rng = pd.date_range('2016 Jul 1', periods = 10, freq = 'D')
rng
pd.Series(range(len(rng)), index = rng)
代码语言:javascript复制2016-07-01 0
2016-07-02 1
2016-07-03 2
2016-07-04 3
2016-07-05 4
2016-07-06 5
2016-07-07 6
2016-07-08 7
2016-07-09 8
2016-07-10 9
Freq: D, dtype: int64
代码语言:javascript复制periods = [pd.Period('2016-01'), pd.Period('2016-02'), pd.Period('2016-03')]
ts = pd.Series(np.random.randn(len(periods)), index = periods)
ts
代码语言:javascript复制2016-01 -0.559086
2016-02 -1.021617
2016-03 0.944657
Freq: M, dtype: float64
代码语言:javascript复制type(ts.index)
代码语言:javascript复制pandas.core.indexes.period.PeriodIndex
时间戳和时间周期可以转换
代码语言:javascript复制ts = pd.Series(range(10), pd.date_range('07-10-16 8:00', periods = 10, freq = 'H'))
ts
代码语言:javascript复制2016-07-10 08:00:00 0
2016-07-10 09:00:00 1
2016-07-10 10:00:00 2
2016-07-10 11:00:00 3
2016-07-10 12:00:00 4
Freq: H, dtype: int64
代码语言:javascript复制ts_period = ts.to_period()
ts_period
代码语言:javascript复制2016-07-10 08:00 0
2016-07-10 09:00 1
2016-07-10 10:00 2
2016-07-10 11:00 3
2016-07-10 12:00 4
2016-07-10 13:00 5
2016-07-10 14:00 6
2016-07-10 15:00 7
2016-07-10 16:00 8
2016-07-10 17:00 9
Freq: H, dtype: int64
代码语言:javascript复制ts_period['2016-07-10 08:30':'2016-07-10 11:45']
代码语言:javascript复制2016-07-10 08:00 0
2016-07-10 09:00 1
2016-07-10 10:00 2
2016-07-10 11:00 3
Freq: H, dtype: int64
代码语言:javascript复制ts['2016-07-10 08:30':'2016-07-10 11:45']
代码语言:javascript复制2016-07-10 09:00:00 1
2016-07-10 10:00:00 2
2016-07-10 11:00:00 3
Freq: H, dtype: int64
数据重采样
- 时间数据由一个频率转换到另一个频率
- 降采样
- 升采样
import pandas as pd
import numpy as np
rng = pd.date_range('1/1/2011', periods=90, freq='D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
ts.head()
代码语言:javascript复制2011-01-01 -0.225796
2011-01-02 0.890969
2011-01-03 -0.343222
2011-01-04 -0.884985
2011-01-05 0.859801
Freq: D, dtype: float64
重采样resample
- 以月为单位
ts.resample('M').sum()
ts.resample("M").sum()
代码语言:javascript复制2011-01-31 -3.221512
2011-02-28 9.660282
2011-03-31 -0.934169
Freq: M, dtype: float64
- 以天为单位
ts.resample('3D').sum()
ts.resample("2D").sum()
代码语言:javascript复制2011-01-01 0.665173
2011-01-03 -1.228207
2011-01-05 1.165821
2011-01-07 -2.507237
Freq: 2D, dtype: float64
- 计算均值
day3Ts = ts.resample('3D').mean()
day3Ts
代码语言:javascript复制2011-01-01 0.107317
2011-01-04 0.093612
2011-01-07 -1.156626
2011-01-10 -0.172981
Freq: 3D, dtype: float64
- resample()重采样和asfreq()频度转换
print(day3Ts.resample('D').asfreq())
代码语言:javascript复制2011-01-01 0.107317
2011-01-02 NaN
2011-01-03 NaN
2011-01-04 0.093612
2011-01-05 NaN
...
2011-03-25 NaN
2011-03-26 0.804057
2011-03-27 NaN
2011-03-28 NaN
2011-03-29 -0.200729
Freq: D, Length: 88, dtype: float64
代码语言:javascript复制print(day3Ts.resample('D'))
代码语言:javascript复制DatetimeIndexResampler [freq=<Day>, axis=0, closed=left, label=left, convention=start, base=0]
插值方法
升采样可能出现问题,对于控制使用插值方法
- ffill 空值取前面的值 bfill 空值取后面的值 interpolate 线性取值
day3Ts.resample('D').ffill(2)
代码语言:javascript复制2011-01-01 0.107317
2011-01-02 0.107317
2011-01-03 0.107317
2011-01-04 0.093612
2011-01-05 0.093612
...
2011-03-25 -0.045712
2011-03-26 0.804057
2011-03-27 0.804057
2011-03-28 0.804057
2011-03-29 -0.200729
Freq: D, Length: 88, dtype: float64
代码语言:javascript复制day3Ts.resample('D').bfill(1)
代码语言:javascript复制2011-01-01 0.107317
2011-01-02 NaN
2011-01-03 0.093612
2011-01-04 0.093612
2011-01-05 NaN
...
2011-03-25 0.804057
2011-03-26 0.804057
2011-03-27 NaN
2011-03-28 -0.200729
2011-03-29 -0.200729
Freq: D, Length: 88, dtype: float64
代码语言:javascript复制day3Ts.resample('D').interpolate("linear")
代码语言:javascript复制2011-01-01 0.107317
2011-01-02 0.102749
2011-01-03 0.098180
2011-01-04 0.093612
2011-01-05 -0.323134
...
2011-03-25 0.520801
2011-03-26 0.804057
2011-03-27 0.469128
2011-03-28 0.134200
2011-03-29 -0.200729
Freq: D, Length: 88, dtype: float64
到这里就结束了,如果对你有帮助你,欢迎点赞关注,你的点赞对我很重要