歡迎加入本課程!

使用 Python 視覺化時間序列資料

Thomas Vincent

Head of Data Science, Getty Images

先修要求

使用 Python 視覺化時間序列資料

資料科學中的時間序列

  • 時間序列是儲存與分析多種資料的基本方式
  • 財務、天氣與裝置資料最適合用時間序列處理
使用 Python 視覺化時間序列資料

資料科學中的時間序列

時間序列資料範例

使用 Python 視覺化時間序列資料

課程總覽

  • 第 1 章:入門與自訂你的第一張時間序列圖
  • 第 2 章:摘要與描述時間序列資料
  • 第 3 章:進階時間序列分析
  • 第 4 章:處理多個時間序列
  • 第 5 章:案例研究
使用 Python 視覺化時間序列資料

使用 Pandas 讀取資料

import pandas as pd

df = pd.read_csv('ch2_co2_levels.csv') print(df)
       datestamp    co2
0     1958-03-29  316.1
1     1958-04-05  317.3
2     1958-04-12  317.6
...
...
...
2281  2001-12-15  371.2
2282  2001-12-22  371.3
2283  2001-12-29  371.5
使用 Python 視覺化時間序列資料

用 Pandas 預覽資料

print(df.head(n=5))
    datestamp    co2
0  1958-03-29  316.1
1  1958-04-05  317.3
2  1958-04-12  317.6
3  1958-04-19  317.5
4  1958-04-26  316.4
print(df.tail(n=5))
       datestamp    co2
2279  2001-12-01  370.3
2280  2001-12-08  370.8
2281  2001-12-15  371.2
2282  2001-12-22  371.3
2283  2001-12-29  371.5
使用 Python 視覺化時間序列資料

用 Pandas 檢查資料型別

print(df.dtypes)
datestamp     object
co2          float64
dtype: object
使用 Python 視覺化時間序列資料

處理日期

pandas 中處理時間序列,日期欄位需要是 datetime64 型別。

pd.to_datetime(['2009/07/31', 'test'])
ValueError: Unknown string format
pd.to_datetime(['2009/07/31', 'test'], errors='coerce')
DatetimeIndex(['2009-07-31', 'NaT'], 
    dtype='datetime64[ns]', freq=None)
使用 Python 視覺化時間序列資料

開始吧!

使用 Python 視覺化時間序列資料

Preparing Video For Download...