TransWikia.com

Is there any way to cast timedelta64 to int64 in numba?

Stack Overflow Asked by Anton Philippoff on November 17, 2021

For example, I have a piece of code:

import numpy as np
from numba import njit

d = np.array(['2001-01-01T12:00', '2002-02-03T13:56:03.172'],
              dtype='datetime64')

@njit
def datetime_operand(date):
    x = date[1] - date[0]
    return x


datetime_operand(d) // the result is numpy.timedelta64(34394163172,'ms')

Simple option to type np.int64(x) doesn’t help.

2 Answers

Actually, the answer is:

import numpy as np
dt64 = np.datetime64('2009-01-01')
print((dt64 — np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's'))

Answered by Anton Philippoff on November 17, 2021

I am not sure about the result you look for, but I suppose pandas can help here:

import pandas
@njit
def datetime_operand(date):
    df = pandas.DataFrame(d.T)
    df[0]=pd.to_datetime(df[0])
    return (df[0].iloc[1]-df[0].iloc[0]).to_numpy()

The result is like this:

datetime_operand(d)
umpy.timedelta64(34394163172,'ms')

Answered by Nose on November 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP