欢迎学习本课程!

用 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...