1.我們在引入庫的時候,還需要用import datetime as dt
引入datetime庫,因爲後面的操作很多都和日期有關,很有可能會用到和datetime有關的函數。
2.date_range()
:可以指定開始時間與週期
- H:小時
- D:天
- M:月
3.pd.date_range("Y-M-D",periods=t,freq="kD")
:生成開始時間爲Y-M-D,週期爲k天的t個日期,D也可以換成其他表示時間的字母。
4.pd.Series(np.random.randn(n),index=pd.date_range(dt.datetime(Y,M,D),periods=t))
:生成以時間爲索引的n個隨機數。
除了可以進行索引,我們還可以對日期進行切片操作從而得到數據。
5.truncate()
:過濾操作,參數可以爲before=
或after=
,過濾掉參數所指定的日期之前或之後的數據,不包括參數內指定的數據。
6.pd.Timestamp()
:指定時間戳。
7.pd.Timedelta("n days")
:指定時間偏移量。
8.時間戳和時間區間還可以指定更多的細節。
9.resample()
:數據重採樣函數,可以後接一個函數求數據的統計量。
10.由於在升採樣的過程中會出現空值,所以需要進行插值以填補空缺。
ffill(n)
:填充從前往後的n個空值,空值取前面的值。bfill(n)
:填充從前往後的n個空值,空值取後面的值。interpolate()
:線性取值。
11.df.rolling(window = n)
:指定以n爲週期的滑動窗口,所謂滑動窗口就是縮小數據所在的區間進而求得更爲精確的統計量。
12.可以根據均值繪製出一個較爲平穩的時間序列圖像。
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
df_time = pd.Series(np.random.randn(600),index=pd.date_range("2018-1-1",periods=600,freq="D"))
r = df_time.rolling(window=10)
mean_r = df_time.rolling(window=10).mean()
fig = plt.figure(figsize=(15,5))
df_time.plot(style="c-.")
mean_r.plot(style="r-")