【问题标题】:calculate the number of years in a csv file using pandas使用 pandas 计算 csv 文件中的年数
【发布时间】:2017-11-07 14:14:17
【问题描述】:

我有一个包含多年数据的 csv 文件,我正在使用 pandas 来读取它。 我的主要要求是如何计算从最大日期和最小日期开始的年数。

timestamp,heure,lat,lon,impact,type
2007-01-01 00:00:00,13:58:43,33.837,-9.205,10.3,1
2007-01-02 00:00:00,00:07:28,34.5293,-10.2384,17.7,1
2007-01-02 00:00:00,23:01:03,35.0617,-1.435,-17.1,2
2007-01-03 00:00:00,01:14:29,36.5685,0.9043,36.8,1
2007-01-03 00:00:00,05:03:51,34.1919,-12.5061,-48.9,1

我的操作如下:

data['time'] = pd.to_datetime(data['time'])
DateMax = data.index.max()
DateMin = data.index.min()
NByears = (DateMax - DateMin).astype('datetime64[Y]')

但它不起作用,有什么想法吗?

【问题讨论】:

    标签: python-2.7 pandas datetime


    【解决方案1】:

    看来你需要先转换成DatetimeIndex.year,得到minmax,最后减去:

    DateMax = data.index.year.max()
    DateMin = data.index.year.min()
    NByears = (DateMax - DateMin)
    

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2018-09-05
      • 1970-01-01
      • 2021-09-21
      • 2019-05-20
      相关资源
      最近更新 更多