从numpy datetime64获取年,月或日 [英] Get year, month or day from numpy datetime64
本文介绍了从numpy datetime64获取年,月或日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dates = np.datetime64(['2010-10-17' '2011-05-13',2012-01-15])
有更好的方式比循环每个元素只是为了获得年龄的np.array:
years = f(dates)
#输出:
数组([2010,2011,2012],dtype = int8)#or dtype = string
我使用的是稳定的numpy版本1.6.2。
解决方案
由于datetime在numpy中不稳定,我会使用大熊猫为此:
在[52]中:将大熊猫导入为pd
在[53]中: dates = pd.DatetimeIndex(['2010-10-17','2011-05-13',2012-01-15])
在[54]:dates.year
Out [54]:array([2010,2011,2012],dtype = int32)
熊猫在内部使用numpy datetime,但似乎避免了短缺,numpy已经到现在为止。
I have an array of datetime64 type:
dates = np.datetime64(['2010-10-17', '2011-05-13', "2012-01-15"])
Is there a better way than looping through each element just to get np.array of years:
years = f(dates)
#output:
array([2010, 2011, 2012], dtype=int8) #or dtype = string
I'm using stable numpy version 1.6.2.
解决方案
As datetime is not stable in numpy I would use pandas for this:
In [52]: import pandas as pd
In [53]: dates = pd.DatetimeIndex(['2010-10-17', '2011-05-13', "2012-01-15"])
In [54]: dates.year
Out[54]: array([2010, 2011, 2012], dtype=int32)
Pandas uses numpy datetime internally, but seems to avoid the shortages, that numpy has up to now.
这篇关于从numpy datetime64获取年,月或日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文