zoukankan      html  css  js  c++  java
  • TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

    这个问题说的很清楚,就是类型不对,需要转化类型,首先讲一下这个问题是在使用pandas的resample函数激发的,官方文档解释的较为清楚,如下:

    Convenience method for frequency conversion and resampling of time series. Object must have a datetime-like index (DatetimeIndex, PeriodIndex, or TimedeltaIndex), or pass datetime-like values to the on or level keyword.

    这个函数的目的主要是重新选择时间频率的,一组数据的频率既可以变大也可以变小,完全根据需求来,没有的数据就会变成NaN。问题是resample的数据必须是datetime-like index,而pandas的数据是DateFrame类型的,这个时候啊,该数据结构类似一种词典类型,给提供了index,values,columns等基本的属性。此时,只需要将数据的索引变成datetime类型的即可。如下:

    y.index = pd.to_datetime(y.index)

    这个时候我们看一下数据类型从DateFrame变成了Series,可以说是解决了大问题,其实之前的博客写过,Series是很方便的,但也不是说DateFrame不方便,稍稍处理一下功能更多。

    另外还有一个iterrows的迭代器,也很方便,官方介绍是这样的:

    DataFrame.iterrows() Iterate over DataFrame rows as (index, Series) pairs.

    这个也很好啊,这样对值的访问就更加方便了,因为Series支持label索引式的访问。

  • 相关阅读:
    分布式事务的MQ实现
    zipkin 介绍入门
    线程5问?
    微服务分布式系统架构,转载,备份
    微服务,分布式架构
    史上最全 40 道 Dubbo 面试题及答案,看完碾压面试官!
    Tomcat优化
    windows 下安装kafka
    经典台词
    分布式锁3种实现
  • 原文地址:https://www.cnblogs.com/cvtoEyes/p/9578668.html
Copyright © 2011-2022 走看看