pandas关于date的处理与读取

在使用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.
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值