Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get year, month or day from numpy datetime64

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.

like image 542
enedene Avatar asked Nov 30 '12 16:11

enedene


People also ask

How can I get year of NumPy datetime64?

datetime64 to float-year. year = dt64. astype('M8[Y]') contains just the year. If you want a float array of that, do 1970 + year.

Is datetime64 the same as datetime?

NumPy has no separate date and time objects, just a single datetime64 object to represent a single moment in time. The datetime module's datetime object has microsecond precision (one-millionth of a second). NumPy's datetime64 object allows you to set its precision from hours all the way to attoseconds (10 ^ -18).

What is NP datetime64?

datetime64() method, we can get the date in a numpy array in a particular format i.e year-month-day by using numpy. datetime64() method. Syntax : numpy.datetime64(date) Return : Return the date in a format 'yyyy-mm-dd'.

What is NumPy Timedelta?

Datetime and Timedelta ArithmeticNumPy allows the subtraction of two datetime values, an operation which produces a number with a time unit. Because NumPy doesn't have a physical quantities system in its core, the timedelta64 data type was created to complement datetime64 .


2 Answers

I find the following tricks give between 2x and 4x speed increase versus the pandas method described in this answer (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 
like image 52
Anon Avatar answered Oct 15 '22 00:10

Anon


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.

like image 43
bmu Avatar answered Oct 15 '22 00:10

bmu