4

I have a numpy array x of dtype np.int64 representing nanoseconds. I'm able to convert this into a numpy array of dtype np.datetime64 with the following code:

np.array([np.datetime64(int(a), 'ns') for a in x])

Is there a better way to do this, that avoids python list comprehension?

1 Answer 1

5

You can do:

x = np.array([1e9, 5e9, 1e10], dtype=np.int64)

x.astype('datetime64[ns]')

Output:

array(['1970-01-01T00:00:01.000000000', '1970-01-01T00:00:05.000000000',
       '1970-01-01T00:00:10.000000000'], dtype='datetime64[ns]')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.