Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does numpy.datetime64 give an error with an array of years?

I am working with some data that has the date in the format 2017.08333. In the process of converting this to datetime objects I got an error when trying to convert the integer years to datetime.

np.datetime64(['2017','2018'])

The above fails with the error:Could not convert object to NumPy datetime

I was under the impression numpy functions were vectorized. Can someone explain why this doesn't work and how I can solve this? I have 3,000 dates to convert so something that doesn't involve a loop is ideal.

like image 589
Gabe Avatar asked Oct 27 '25 08:10

Gabe


1 Answers

To create an array of dates, you could do:

import numpy as np

print(np.array(['2018', '2017'], dtype=np.datetime64))

Output

['2018' '2017']

A more detailed explanation can be found here

like image 96
Dani Mesejo Avatar answered Oct 29 '25 21:10

Dani Mesejo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!