Python 資料分析第五期–簡述時間序列資料分析
文章目錄
- Python 資料分析第五期--簡述時間序列資料分析
-
- 1.Python 的日期和時間處理操作
-
- 1.1 時間序列分類
- 1.2 datetime 子產品
- 1.3 str -> datetime
- 2.Pandas 的日期和時間處理操作
-
- 2.1 Pandas的時間序列處理
- 2.2 索引
- 2.3 過濾
- 2.4 生成日期範圍
- 2.5 頻率與偏移量
- 2.6 移動資料
- 3.時間資料重采樣
-
- 3.1 resample
- 3.2 降采樣
- 3.3 升采樣
- 4.時間序列資料統計-滑動視窗
1.Python 的日期和時間處理操作
1.1 時間序列分類
時間戳(timestamp),特定的時刻。
固定周期(period),某月或某年。
時間間隔(interval),由起始時間戳和結束時間戳表示。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPB1UeRpmTxsmeOpHOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2MjMwAjNxMjM1EjMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
1.2 datetime 子產品
In [1]:
from datetime import datetime
In [2]:
now = datetime.now()
print(now)
out:
In [3]:
out:
In [4]:
diff = datetime(2017, 3, 4, 17) - datetime(2017, 2, 18, 15)
print(type(diff))
print(diff)
print('經曆了{}天, {}秒。'.format(diff.days, diff.seconds))
<class 'datetime.timedelta'>
14 days, 2:00:00
經曆了14天, 7200秒。
1.3 str -> datetime
In [7]:
# strptime
dt_str = '2017-02-18'
dt_obj2 = datetime.strptime(dt_str, '%Y-%m-%d')
print(type(dt_obj2))
print(dt_obj2)
<class 'datetime.datetime'>
2017-02-18 00:00:00
In [8]:
# dateutil.parser.parse
from dateutil.parser import parse
dt_str2 = '2017/02/18'
dt_obj3 = parse(dt_str2)
print(type(dt_obj3))
print(dt_obj3)
<class 'datetime.datetime'>
2017-02-18 00:00:00
In [9]:
# pd.to_datetime
import pandas as pd
s_obj = pd.Series(['2017/02/18', '2017/02/19', '2017-02-25', '2017-02-26'], name='course_time')
print(s_obj)
0 2017/02/18
1 2017/02/19
2 2017-02-25
3 2017-02-26
Name: course_time, dtype: object
In [10]:
print(s_obj2)
0 2017-02-18
1 2017-02-19
2 2017-02-25
3 2017-02-26
Name: course_time, dtype: datetime64[ns]
In [11]:
# 處理缺失值
s_obj3 = pd.Series(['2017/02/18', '2017/02/19', '2017-02-25', '2017-02-26'] + [None],
name='course_time')
print(s_obj3)
0 2017/02/18
1 2017/02/19
2 2017-02-25
3 2017-02-26
4 None
Name: course_time, dtype: object
In [12]:
s_obj4 = pd.to_datetime(s_obj3)
print(s_obj4) # NAT-> Not a Time
0 2017-02-18
1 2017-02-19
2 2017-02-25
3 2017-02-26
4 NaT
Name: course_time, dtype: datetime64[ns]
2.Pandas 的日期和時間處理操作
2.1 Pandas的時間序列處理
建立
from datetime import datetime
import pandas as pd
import numpy as np
# 指定index為datetime的list
date_list = [datetime(2017, 2, 18), datetime(2017, 2, 19),
datetime(2017, 2, 25), datetime(2017, 2, 26),
datetime(2017, 3, 4), datetime(2017, 3, 5)]
time_s = pd.Series(np.random.randn(6), index=date_list)
print(time_s)
print(type(time_s.index))
2017-02-18 -0.543551
2017-02-19 -0.759103
2017-02-25 0.058956
2017-02-26 0.275448
2017-03-04 -0.957346
2017-03-05 -1.143108
dtype: float64
<class 'pandas.tseries.index.DatetimeIndex'>
<class 'pandas.core.series.Series'>
# pd.date_range()
dates = pd.date_range('2017-02-18', # 起始日期
periods=5, # 周期
freq='W-SAT') # 頻率
print(dates)
print(pd.Series(np.random.randn(5), index=dates))
DatetimeIndex(['2017-02-18', '2017-02-25', '2017-03-04', '2017-03-11',
'2017-03-18'],
dtype='datetime64[ns]', freq='W-SAT')
2017-02-18 -0.921937
2017-02-25 0.722167
2017-03-04 -0.171531
2017-03-11 -1.104664
2017-03-18 1.259994
Freq: W-SAT, dtype: float64
2.2 索引
# 索引位置
print(time_s[0])
-0.543550683904
# 索引值
print(time_s[datetime(2017, 2, 18)])
-0.543550683904
# 可以被解析的日期字元串
print(time_s['2017/02/18'])
-0.543550683904
# 按“年份”、“月份”索引
print(time_s['2017-2'])
2017-02-18 -0.543551
2017-02-19 -0.759103
2017-02-25 0.058956
2017-02-26 0.275448
dtype: float64
# 切片操作
print(time_s['2017-2-26':])
2017-02-26 0.275448
2017-03-04 -0.957346
2017-03-05 -1.143108
dtype: float64
2.3 過濾
Out[16]:
2017-02-25 0.058956
2017-02-26 0.275448
2017-03-04 -0.957346
2017-03-05 -1.143108
dtype: float64
Out[17]:
2017-02-18 -0.543551
2017-02-19 -0.759103
2017-02-25 0.058956
dtype: float64
2.4 生成日期範圍
# 傳入開始、結束日期,預設生成的該時間段的時間點是按天計算的
date_index = pd.date_range('2017/02/18', '2017/03/18')
print(date_index)
DatetimeIndex(['2017-02-18', '2017-02-19', '2017-02-20', '2017-02-21',
'2017-02-22', '2017-02-23', '2017-02-24', '2017-02-25',
'2017-02-26', '2017-02-27', '2017-02-28', '2017-03-01',
'2017-03-02', '2017-03-03', '2017-03-04', '2017-03-05',
'2017-03-06', '2017-03-07', '2017-03-08', '2017-03-09',
'2017-03-10', '2017-03-11', '2017-03-12', '2017-03-13',
'2017-03-14', '2017-03-15', '2017-03-16', '2017-03-17',
'2017-03-18'],
dtype='datetime64[ns]', freq='D')
# 隻傳入開始或結束日期,還需要傳入時間段
print(pd.date_range(start='2017/02/18', periods=10))
DatetimeIndex(['2017-02-18', '2017-02-19', '2017-02-20', '2017-02-21',
'2017-02-22', '2017-02-23', '2017-02-24', '2017-02-25',
'2017-02-26', '2017-02-27'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-03-09', '2017-03-10', '2017-03-11', '2017-03-12',
'2017-03-13', '2017-03-14', '2017-03-15', '2017-03-16',
'2017-03-17', '2017-03-18'],
dtype='datetime64[ns]', freq='D')
# 規範化時間戳
print(pd.date_range(start='2017/02/18 12:13:14', periods=10))
print(pd.date_range(start='2017/02/18 12:13:14', periods=10, normalize=True))
DatetimeIndex(['2017-02-18 12:13:14', '2017-02-19 12:13:14',
'2017-02-20 12:13:14', '2017-02-21 12:13:14',
'2017-02-22 12:13:14', '2017-02-23 12:13:14',
'2017-02-24 12:13:14', '2017-02-25 12:13:14',
'2017-02-26 12:13:14', '2017-02-27 12:13:14'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-02-18', '2017-02-19', '2017-02-20', '2017-02-21',
'2017-02-22', '2017-02-23', '2017-02-24', '2017-02-25',
'2017-02-26', '2017-02-27'],
dtype='datetime64[ns]', freq='D')
2.5 頻率與偏移量
DatetimeIndex(['2017-02-18', '2017-02-20', '2017-02-22', '2017-02-24',
'2017-02-26', '2017-02-28', '2017-03-02', '2017-03-04',
'2017-03-06', '2017-03-08', '2017-03-10', '2017-03-12',
'2017-03-14', '2017-03-16', '2017-03-18'],
dtype='datetime64[ns]', freq='2D')
# 偏移量通過加法連接配接
sum_offset = pd.tseries.offsets.Week(2) + pd.tseries.offsets.Hour(12)
print(sum_offset)
print(pd.date_range('2017/02/18', '2017/03/18', freq=sum_offset))
14 days 12:00:00
DatetimeIndex(['2017-02-18 00:00:00', '2017-03-04 12:00:00'], dtype='datetime64[ns]', freq='348H')
2.6 移動資料
ts = pd.Series(np.random.randn(5), index=pd.date_range('20170218', periods=5, freq='W-SAT'))
print(ts)
2017-02-18 0.400190
2017-02-25 1.495394
2017-03-04 -1.331107
2017-03-11 2.943859
2017-03-18 0.813070
Freq: W-SAT, dtype: float64
print(ts.shift(1))
print(ts.shift(-1))
2017-02-18 NaN
2017-02-25 0.400190
2017-03-04 1.495394
2017-03-11 -1.331107
2017-03-18 2.943859
Freq: W-SAT, dtype: float64
2017-02-18 1.495394
2017-02-25 -1.331107
2017-03-04 2.943859
2017-03-11 0.813070
2017-03-18 NaN
Freq: W-SAT, dtype: float64
3.時間資料重采樣
重采樣(resampling)
将時間序列從一個頻率轉換到另一個頻率的過程,需要聚合。
3.1 resample
import pandas as pd
import numpy as np
date_rng = pd.date_range('20170101', periods=100, freq='D')
ser_obj = pd.Series(range(len(date_rng)), index=date_rng)
print(ser_obj.head(10))
2017-01-01 0
2017-01-02 1
2017-01-03 2
2017-01-04 3
2017-01-05 4
2017-01-06 5
2017-01-07 6
2017-01-08 7
2017-01-09 8
2017-01-10 9
Freq: D, dtype: int64
# 統計每個月的資料總和
resample_month_sum = ser_obj.resample('M').sum()
# 統計每個月的資料平均
resample_month_mean = ser_obj.resample('M').mean()
print('按月求和:', resample_month_sum)
print('按月求均值:', resample_month_mean)
按月求和: 2017-01-31 465
2017-02-28 1246
2017-03-31 2294
2017-04-30 945
Freq: M, dtype: int64
按月求均值: 2017-01-31 15.0
2017-02-28 44.5
2017-03-31 74.0
2017-04-30 94.5
Freq: M, dtype: float64
3.2 降采樣
# 将資料聚合到5天的頻率
five_day_sum_sample = ser_obj.resample('5D').sum()
five_day_mean_sample = ser_obj.resample('5D').mean()
five_day_ohlc_sample = ser_obj.resample('5D').ohlc()
print('降采樣,sum')
print(five_day_sum_sample)
降采樣,sum
2017-01-01 10
2017-01-06 35
2017-01-11 60
2017-01-16 85
2017-01-21 110
2017-01-26 135
2017-01-31 160
2017-02-05 185
2017-02-10 210
2017-02-15 235
2017-02-20 260
2017-02-25 285
2017-03-02 310
2017-03-07 335
2017-03-12 360
2017-03-17 385
2017-03-22 410
2017-03-27 435
2017-04-01 460
2017-04-06 485
Freq: 5D, dtype: int64
print('降采樣,mean')
print(five_day_mean_sample)
降采樣,mean
2017-01-01 2
2017-01-06 7
2017-01-11 12
2017-01-16 17
2017-01-21 22
2017-01-26 27
2017-01-31 32
2017-02-05 37
2017-02-10 42
2017-02-15 47
2017-02-20 52
2017-02-25 57
2017-03-02 62
2017-03-07 67
2017-03-12 72
2017-03-17 77
2017-03-22 82
2017-03-27 87
2017-04-01 92
2017-04-06 97
Freq: 5D, dtype: int64
print('降采樣,ohlc')
print(five_day_ohlc_sample)
降采樣,ohlc
open high low close
2017-01-01 0 4 0 4
2017-01-06 5 9 5 9
2017-01-11 10 14 10 14
2017-01-16 15 19 15 19
2017-01-21 20 24 20 24
2017-01-26 25 29 25 29
2017-01-31 30 34 30 34
2017-02-05 35 39 35 39
2017-02-10 40 44 40 44
2017-02-15 45 49 45 49
2017-02-20 50 54 50 54
2017-02-25 55 59 55 59
2017-03-02 60 64 60 64
2017-03-07 65 69 65 69
2017-03-12 70 74 70 74
2017-03-17 75 79 75 79
2017-03-22 80 84 80 84
2017-03-27 85 89 85 89
2017-04-01 90 94 90 94
2017-04-06 95 99 95 99
# 使用groupby降采樣
print(ser_obj.groupby(lambda x: x.month).sum())
1 465
2 1246
3 2294
4 945
dtype: int64
0 750
1 665
2 679
3 693
4 707
5 721
6 735
dtype: int64
3.3 升采樣
df = pd.DataFrame(np.random.randn(5, 3),
index=pd.date_range('20170101', periods=5, freq='W-MON'),
columns=['S1', 'S2', 'S3'])
print(df)
S1 S2 S3
2017-01-02 0.684339 -1.995068 -0.765862
2017-01-09 0.797372 -1.686004 0.315683
2017-01-16 -0.283060 -0.234096 0.111255
2017-01-23 0.587643 -0.272558 -2.065481
2017-01-30 -0.278977 -0.646818 0.282946
# 直接重采樣會産生空值
print(df.resample('D').asfreq())
S1 S2 S3
2017-01-02 0.684339 -1.995068 -0.765862
2017-01-03 NaN NaN NaN
2017-01-04 NaN NaN NaN
2017-01-05 NaN NaN NaN
2017-01-06 NaN NaN NaN
2017-01-07 NaN NaN NaN
2017-01-08 NaN NaN NaN
2017-01-09 0.797372 -1.686004 0.315683
2017-01-10 NaN NaN NaN
2017-01-11 NaN NaN NaN
2017-01-12 NaN NaN NaN
2017-01-13 NaN NaN NaN
2017-01-14 NaN NaN NaN
2017-01-15 NaN NaN NaN
2017-01-16 -0.283060 -0.234096 0.111255
2017-01-17 NaN NaN NaN
2017-01-18 NaN NaN NaN
2017-01-19 NaN NaN NaN
2017-01-20 NaN NaN NaN
2017-01-21 NaN NaN NaN
2017-01-22 NaN NaN NaN
2017-01-23 0.587643 -0.272558 -2.065481
2017-01-24 NaN NaN NaN
2017-01-25 NaN NaN NaN
2017-01-26 NaN NaN NaN
2017-01-27 NaN NaN NaN
2017-01-28 NaN NaN NaN
2017-01-29 NaN NaN NaN
2017-01-30 -0.278977 -0.646818 0.282946
#ffill
print(df.resample('D').ffill(2))
S1 S2 S3
2017-01-02 0.684339 -1.995068 -0.765862
2017-01-03 0.684339 -1.995068 -0.765862
2017-01-04 0.684339 -1.995068 -0.765862
2017-01-05 NaN NaN NaN
2017-01-06 NaN NaN NaN
2017-01-07 NaN NaN NaN
2017-01-08 NaN NaN NaN
2017-01-09 0.797372 -1.686004 0.315683
2017-01-10 0.797372 -1.686004 0.315683
2017-01-11 0.797372 -1.686004 0.315683
2017-01-12 NaN NaN NaN
2017-01-13 NaN NaN NaN
2017-01-14 NaN NaN NaN
2017-01-15 NaN NaN NaN
2017-01-16 -0.283060 -0.234096 0.111255
2017-01-17 -0.283060 -0.234096 0.111255
2017-01-18 -0.283060 -0.234096 0.111255
2017-01-19 NaN NaN NaN
2017-01-20 NaN NaN NaN
2017-01-21 NaN NaN NaN
2017-01-22 NaN NaN NaN
2017-01-23 0.587643 -0.272558 -2.065481
2017-01-24 0.587643 -0.272558 -2.065481
2017-01-25 0.587643 -0.272558 -2.065481
2017-01-26 NaN NaN NaN
2017-01-27 NaN NaN NaN
2017-01-28 NaN NaN NaN
2017-01-29 NaN NaN NaN
2017-01-30 -0.278977 -0.646818 0.282946
S1 S2 S3
2017-01-02 0.684339 -1.995068 -0.765862
2017-01-03 0.797372 -1.686004 0.315683
2017-01-04 0.797372 -1.686004 0.315683
2017-01-05 0.797372 -1.686004 0.315683
2017-01-06 0.797372 -1.686004 0.315683
2017-01-07 0.797372 -1.686004 0.315683
2017-01-08 0.797372 -1.686004 0.315683
2017-01-09 0.797372 -1.686004 0.315683
2017-01-10 -0.283060 -0.234096 0.111255
2017-01-11 -0.283060 -0.234096 0.111255
2017-01-12 -0.283060 -0.234096 0.111255
2017-01-13 -0.283060 -0.234096 0.111255
2017-01-14 -0.283060 -0.234096 0.111255
2017-01-15 -0.283060 -0.234096 0.111255
2017-01-16 -0.283060 -0.234096 0.111255
2017-01-17 0.587643 -0.272558 -2.065481
2017-01-18 0.587643 -0.272558 -2.065481
2017-01-19 0.587643 -0.272558 -2.065481
2017-01-20 0.587643 -0.272558 -2.065481
2017-01-21 0.587643 -0.272558 -2.065481
2017-01-22 0.587643 -0.272558 -2.065481
2017-01-23 0.587643 -0.272558 -2.065481
2017-01-24 -0.278977 -0.646818 0.282946
2017-01-25 -0.278977 -0.646818 0.282946
2017-01-26 -0.278977 -0.646818 0.282946
2017-01-27 -0.278977 -0.646818 0.282946
2017-01-28 -0.278977 -0.646818 0.282946
2017-01-29 -0.278977 -0.646818 0.282946
2017-01-30 -0.278977 -0.646818 0.282946
S1 S2 S3
2017-01-02 0.684339 -1.995068 -0.765862
2017-01-03 0.684339 -1.995068 -0.765862
2017-01-04 0.684339 -1.995068 -0.765862
2017-01-05 0.684339 -1.995068 -0.765862
2017-01-06 0.684339 -1.995068 -0.765862
2017-01-07 0.684339 -1.995068 -0.765862
2017-01-08 0.684339 -1.995068 -0.765862
2017-01-09 0.797372 -1.686004 0.315683
2017-01-10 0.797372 -1.686004 0.315683
2017-01-11 0.797372 -1.686004 0.315683
2017-01-12 0.797372 -1.686004 0.315683
2017-01-13 0.797372 -1.686004 0.315683
2017-01-14 0.797372 -1.686004 0.315683
2017-01-15 0.797372 -1.686004 0.315683
2017-01-16 -0.283060 -0.234096 0.111255
2017-01-17 -0.283060 -0.234096 0.111255
2017-01-18 -0.283060 -0.234096 0.111255
2017-01-19 -0.283060 -0.234096 0.111255
2017-01-20 -0.283060 -0.234096 0.111255
2017-01-21 -0.283060 -0.234096 0.111255
2017-01-22 -0.283060 -0.234096 0.111255
2017-01-23 0.587643 -0.272558 -2.065481
2017-01-24 0.587643 -0.272558 -2.065481
2017-01-25 0.587643 -0.272558 -2.065481
2017-01-26 0.587643 -0.272558 -2.065481
2017-01-27 0.587643 -0.272558 -2.065481
2017-01-28 0.587643 -0.272558 -2.065481
2017-01-29 0.587643 -0.272558 -2.065481
2017-01-30 -0.278977 -0.646818 0.282946
S1 S2 S3
2017-01-02 0.684339 -1.995068 -0.765862
2017-01-03 0.700487 -1.950916 -0.611356
2017-01-04 0.716634 -1.906764 -0.456849
2017-01-05 0.732782 -1.862612 -0.302343
2017-01-06 0.748929 -1.818460 -0.147836
2017-01-07 0.765077 -1.774308 0.006670
2017-01-08 0.781225 -1.730156 0.161177
2017-01-09 0.797372 -1.686004 0.315683
2017-01-10 0.643025 -1.478589 0.286479
2017-01-11 0.488677 -1.271173 0.257275
2017-01-12 0.334330 -1.063758 0.228071
2017-01-13 0.179982 -0.856343 0.198867
2017-01-14 0.025635 -0.648927 0.169663
2017-01-15 -0.128713 -0.441512 0.140459
2017-01-16 -0.283060 -0.234096 0.111255
2017-01-17 -0.158674 -0.239591 -0.199708
2017-01-18 -0.034288 -0.245085 -0.510670
2017-01-19 0.090098 -0.250580 -0.821632
2017-01-20 0.214485 -0.256074 -1.132594
2017-01-21 0.338871 -0.261569 -1.443556
2017-01-22 0.463257 -0.267063 -1.754518
2017-01-23 0.587643 -0.272558 -2.065481
2017-01-24 0.463840 -0.326024 -1.729991
2017-01-25 0.340037 -0.379489 -1.394502
2017-01-26 0.216234 -0.432955 -1.059012
2017-01-27 0.092432 -0.486421 -0.723523
2017-01-28 -0.031371 -0.539886 -0.388033
2017-01-29 -0.155174 -0.593352 -0.052544
2017-01-30 -0.278977 -0.646818 0.282946
4.時間序列資料統計-滑動視窗
做一個滑動視窗。
在時間視窗上計算各種統計函數
視窗函數
import pandas as pd
import numpy as np
ser_obj = pd.Series(np.random.randn(1000),
index=pd.date_range('20170101', periods=1000))
ser_obj = ser_obj.cumsum()
print(ser_obj.head())
2017-01-01 0.587254
2017-01-02 -0.343997
2017-01-03 0.164495
2017-01-04 0.103552
2017-01-05 0.306241
Freq: D, dtype: float64
2017-01-01 0.587254
2017-01-02 -0.343997
2017-01-03 0.164495
2017-01-04 0.103552
2017-01-05 0.306241
Freq: D, dtype: float64
r_obj = ser_obj.rolling(window=5)
print(r_obj)
Rolling [window=5,center=False,axis=0]
print(r_obj.mean())
# 驗證:
# 前5個資料的均值
#print(ser_obj[0:5].mean())
# 1-6個資料的均值
# print(ser_obj[1:6].mean())
2017-01-01 NaN
2017-01-02 NaN
2017-01-03 NaN
2017-01-04 NaN
2017-01-05 0.163509
2017-01-06 -0.158289
2017-01-07 -0.628267
2017-01-08 -1.276289
2017-01-09 -1.967628
2017-01-10 -2.405591
2017-01-11 -2.582539
2017-01-12 -2.571216
2017-01-13 -2.218030
2017-01-14 -1.859236
2017-01-15 -1.812012
2017-01-16 -1.483824
2017-01-17 -1.110783
2017-01-18 -1.406455
2017-01-19 -1.548476
2017-01-20 -1.323088
2017-01-21 -1.082429
2017-01-22 -0.511615
2017-01-23 0.730885
2017-01-24 1.804945
2017-01-25 2.740901
2017-01-26 3.513198
2017-01-27 3.942432
2017-01-28 4.062054
2017-01-29 4.094543
2017-01-30 4.178053
...
2019-08-29 6.144966
2019-08-30 6.695811
2019-08-31 7.387182
2019-09-01 7.664938
2019-09-02 8.147945
2019-09-03 8.565462
2019-09-04 8.173071
2019-09-05 7.785990
2019-09-06 7.484676
2019-09-07 7.155520
2019-09-08 6.952482
2019-09-09 7.054065
2019-09-10 7.072867
2019-09-11 6.923366
2019-09-12 6.837505
2019-09-13 6.524303
2019-09-14 6.361202
2019-09-15 6.322569
2019-09-16 6.482722
2019-09-17 6.387641
2019-09-18 6.570551
2019-09-19 6.835381
2019-09-20 7.074634
2019-09-21 6.848433
2019-09-22 7.154768
2019-09-23 7.503370
2019-09-24 7.666051
2019-09-25 7.753261
2019-09-26 8.163166
2019-09-27 8.130434
Freq: D, Length: 1000, dtype: float64
# 畫圖檢視
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(15, 5))
ser_obj.plot(style='r--')
ser_obj.rolling(window=10).mean().plot(style='b')
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x1c45b26d0b8>
2017-01-01 NaN
2017-01-02 NaN
2017-01-03 0.163509
2017-01-04 -0.158289
2017-01-05 -0.628267
2017-01-06 -1.276289
2017-01-07 -1.967628
2017-01-08 -2.405591
2017-01-09 -2.582539
2017-01-10 -2.571216
2017-01-11 -2.218030
2017-01-12 -1.859236
2017-01-13 -1.812012
2017-01-14 -1.483824
2017-01-15 -1.110783
2017-01-16 -1.406455
2017-01-17 -1.548476
2017-01-18 -1.323088
2017-01-19 -1.082429
2017-01-20 -0.511615
2017-01-21 0.730885
2017-01-22 1.804945
2017-01-23 2.740901
2017-01-24 3.513198
2017-01-25 3.942432
2017-01-26 4.062054
2017-01-27 4.094543
2017-01-28 4.178053
2017-01-29 4.034678
2017-01-30 4.182262
...
2019-08-29 7.387182
2019-08-30 7.664938
2019-08-31 8.147945
2019-09-01 8.565462
2019-09-02 8.173071
2019-09-03 7.785990
2019-09-04 7.484676
2019-09-05 7.155520
2019-09-06 6.952482
2019-09-07 7.054065
2019-09-08 7.072867
2019-09-09 6.923366
2019-09-10 6.837505
2019-09-11 6.524303
2019-09-12 6.361202
2019-09-13 6.322569
2019-09-14 6.482722
2019-09-15 6.387641
2019-09-16 6.570551
2019-09-17 6.835381
2019-09-18 7.074634
2019-09-19 6.848433
2019-09-20 7.154768
2019-09-21 7.503370
2019-09-22 7.666051
2019-09-23 7.753261
2019-09-24 8.163166
2019-09-25 8.130434
2019-09-26 NaN
2019-09-27 NaN
Freq: D, Length: 1000, dtype: float64
2017-01-21 0.730885
2017-01-22 1.804945
2017-01-23 2.740901
2017-01-24 3.513198
2017-01-25 3.942432
2017-01-26 4.062054
2017-01-27 4.094543
2017-01-28 4.178053
2017-01-29 4.034678
2017-01-30 4.182262
…
2019-08-29 7.387182
2019-08-30 7.664938
2019-08-31 8.147945
2019-09-01 8.565462
2019-09-02 8.173071
2019-09-03 7.785990
2019-09-04 7.484676
2019-09-05 7.155520
2019-09-06 6.952482
2019-09-07 7.054065
2019-09-08 7.072867
2019-09-09 6.923366
2019-09-10 6.837505
2019-09-11 6.524303
2019-09-12 6.361202
2019-09-13 6.322569
2019-09-14 6.482722
2019-09-15 6.387641
2019-09-16 6.570551
2019-09-17 6.835381
2019-09-18 7.074634
2019-09-19 6.848433
2019-09-20 7.154768
2019-09-21 7.503370
2019-09-22 7.666051
2019-09-23 7.753261
2019-09-24 8.163166
2019-09-25 8.130434
2019-09-26 NaN
2019-09-27 NaN
Freq: D, Length: 1000, dtype: float64