How to round up datetime64 values
I have a numpy array from datetime64
, and I would like to round the sub-subset values of the array elements. For example, 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 vectorial method to round to any frequency (minutes, days, etc.).
+4
Yariv
source
to share
2 answers
rounded = numpy.array(myarray, dtype='datetime64[s]')
or
rounded = myarray.astype('datetime64[s]')
This also works within minutes using:
rounded = numpy.array(myarray, dtype='datetime64[m]')
+6
Yariv
source
to share
Another method to handle truncation would be:
rounded = numpy.array((numpy.array(myarray, dtype='datetime64[m]'), dtype='datetime64[ms]')
0
horseshoe
source
to share