在使用pandas读取文件的时候,可以使用index_col将数据的某一列直接设置为列索引;
parse_date这个参数可以将某一列数据自动解析为时间格式。
google = pd.read_csv("../datasets/GOOGL_2006-01-01_to_2018-01-01.csv", index_col='Date', parse_dates=['Date'])
google.head(5)
Open High Low Close Volume Name
Date
2006-01-03 211.47 218.05 209.32 217.83 13137450 GOOGL
2006-01-04 222.17 224.70 220.09 222.84 15292353 GOOGL
2006-01-05 223.22 226.00 220.97 225.85 10815661 GOOGL
2006-01-06 228.66 235.49 226.85 233.06 17759521 GOOGL
2006-01-09 233.44 236.94 230.70 233.68 12795837 GOOGL
humidity = pd.read_csv("../datasets/humidity.csv", index_col='datetime', parse_dates=['datetime'])
humidity.head(4)
Vancouver Portland San Francisco Seattle Los Angeles \
datetime
2012-10-01 12:00:00 NaN NaN NaN NaN NaN
2012-10-01 13:00:00 76.0 81.0 88.0 81.0 88.0
2012-10-01 14:00:00 76.0 80.0 87.0 80.0 88.0
2012-10-01 15:00:00 76.0 80.0 86.0 80.0 88.0
将时间序列转换成特定的频率,下面的.asfreq(‘M’)中,就是将时间序列数据转换成频率为月的
fig, ax = plt.subplots(figsize=(10,4), dpi=300)
humidity["Kansas City"].asfreq('M').plot(ax=ax, c='blue') # asfreq method is used to convert a time series to a specified frequency.