Get year, month or day from numpy datetime64

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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.

回答1:

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.



回答2:

I find the following tricks give between 2x and 4x speed increase versus the pandas method described above (i.e. pd.DatetimeIndex(dates).year etc.). The speed of [dt.year for dt in dates.astype(object)] I find to be similar to the pandas method. Also these tricks can be applied directly to ndarrays of any shape (2D, 3D etc.)

dates = np.arange(np.datetime64('2000-01-01'), np.datetime64('2010-01-01')) years = dates.astype('datetime64[Y]').astype(int) + 1970 months = dates.astype('datetime64[M]').astype(int) % 12 + 1 days = dates - dates.astype('datetime64[M]') + 1 


回答3:

There should be an easier way to do this, but, depending on what you're trying to do, the best route might be to convert to a regular Python datetime object:

datetime64Obj = np.datetime64('2002-07-04T02:55:41-0700') print datetime64Obj.astype(object).year # 2002 print datetime64Obj.astype(object).day # 4 

Based on comments below, this seems to only work in Python 2.7.x not Python 3.x



回答4:

If you upgrade to numpy 1.7 (where datetime is still labled as experimental) the following should work.

dates/np.timedelta64(1,'Y') 


回答5:

There's no direct way to do it yet, unfortunately, but there are a couple indirect ways:

[dt.year for dt in dates.astype(object)] 

or

[datetime.datetime.strptime(repr(d), "%Y-%m-%d %H:%M:%S").year for d in dates] 

both inspired by the examples here.

Both of these work for me on Numpy 1.6.1. You may need to be a bit more careful with the second one, since the repr() for the datetime64 might have a fraction part after a decimal point.



回答6:

Using numpy version 1.10.4 and pandas version 0.17.1,

dates = np.array(['2010-10-17', '2011-05-13', '2012-01-15'], dtype=np.datetime64) pd.to_datetime(dates).year 

I get what you're looking for:

array([2010, 2011, 2012], dtype=int32) 


回答7:

Anon's answer works great for me, but I just need to modify the statement for days

from:

days = dates - dates.astype('datetime64[M]') + 1

to:

days = dates.astype('datetime64[D]') - dates.astype('datetime64[M]') + 1


回答8:

Another possibility is:

np.datetime64(dates,'Y') - returns - numpy.datetime64('2010') 

or

np.datetime64(dates,'Y').astype(int)+1970 - returns - 2010 

but works only on scalar values, won't take array



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!