Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert numpy.timedelta64 to minutes

Tags:

I have a date time column in a Pandas DataFrame and I'd like to convert it to minutes or seconds.

For example: I want to convert 00:27:00 to 27 mins.

example = data['duration'][0] example 

result: numpy.timedelta64(1620000000000,'ns')

What's the best way to achieve this?

like image 409
Behzad Shahbaz Avatar asked Oct 25 '14 19:10

Behzad Shahbaz


People also ask

What is NumPy Timedelta?

NumPy 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 .


1 Answers

Use array.astype() to convert the type of an array safely:

>>> import numpy as np >>> a = np.timedelta64(1620000000000,'ns') >>> a.astype('timedelta64[m]') numpy.timedelta64(27,'m') 
like image 173
sebix Avatar answered Sep 18 '22 14:09

sebix