4
$\begingroup$

I want to convert timedelta64[ns] to int

timedelta64[ns] example : 01:00:00 , 02:00:00 , 03:00:00 , 04:00:00

I want to convert as : 1, 2 , 3 , 4

Help me please T^T

$\endgroup$
  • $\begingroup$ Simply do type casting, float(str()) $\endgroup$ – Aditya May 10 '18 at 6:34
2
$\begingroup$

You can do:

 t, _ , _ = "01:00:00".split(":")

and then:

int(t)

That will return you: 1.

Putting this together as a function would be:

def time_to_integer(time):
    t, _,_ = time.split(":")
    return int(t)

Please note that you should pass the time variable as string. For example:

time_to_integer("02:00:00")

Would return: 2

$\endgroup$
2
$\begingroup$
In [106]: t = pd.to_timedelta(['01:12:34', '02:00:00', '03:00:00', '04:00:00'])

In [107]: t.seconds // 3600
Out[107]: Int64Index([1, 2, 3, 4], dtype='int64')
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.