我有一个包含多年数据的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]')
但它没有用,有什么想法吗?
答案 0 :(得分:1)
您似乎需要先转换为DatetimeIndex.year
,获取min
和max
并最后减去:
DateMax = data.index.year.max()
DateMin = data.index.year.min()
NByears = (DateMax - DateMin)