Numping to digitize using datetime64

I can't 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 produces the following error:

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

Is this the expected behavior?

+5
source share
1 answer

get an i8 representation 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 a 64-bit integer data type, and view creates an idea of ​​the memory of the array.

+4
source

Source: https://habr.com/ru/post/1208539/


All Articles