Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy digitize with datetime64

I can't seem to get numpy.digitize to work with datetime64:

date_bins = np.array([np.datetime64(datetime.datetime(2014, n, 1), 's') for n in range(1,13)])
np.digitize(date_bins, date_bins)

It gives the following error:

TypeError: Cannot cast array data from dtype('<M8[s]') to dtype('float64') according to the rule 'safe'

Is this expected behaviour?

like image 287
acrophobia Avatar asked Dec 07 '14 18:12

acrophobia


1 Answers

get an i8 view of datetime values:

>>> date_bins_i8 = date_bins.view('i8')
>>> np.digitize(date_bins_i8, date_bins_i8)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

i8 is 64-bit integer data type and view constructs a view of the array's memory.

like image 192
behzad.nouri Avatar answered Oct 19 '22 20:10

behzad.nouri