Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round datetime64 values

I have a numpy array of datetime64, and I would like to round off the sub-second values of the array elements. E.g., from 2001-1-1 10:33:32.5 to 2001-1-1 10:33:32.0. I am looking for a vecotrized method.

More generally, I am looking for a vectorized method to round to any frequency (minutes, days, etc.).

like image 469
Yariv Avatar asked Feb 12 '13 15:02

Yariv


1 Answers

rounded = numpy.array(myarray, dtype='datetime64[s]') or rounded = myarray.astype('datetime64[s]')

This also works for minutes by using:

rounded = numpy.array(myarray, dtype='datetime64[m]')
like image 97
Yariv Avatar answered Sep 21 '22 10:09

Yariv