Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot populate NumPy datetime64 arrays

I'm trying to create a NumPy array that will subsequently be populated by some datetime values. I can't seem to make it work.

import numpy as np
t = np.empty(3,dtype='datetime64')
t

I get a TypeError: Invalid datetime unit "generic" in metadata.
Same if I try :

import numpy as np
t = np.empty(3,dtype='datetime64')
t[0] = np.datetime64('2014-12-12 20:20:20')

I get:

TypeError : Cannot cast numpy timedelta64 scalar from metadata [m] to  according to the rule 'same_kind'
like image 497
caliloo Avatar asked Dec 14 '14 11:12

caliloo


People also ask

What is datetime64 in Numpy?

datetime64() method, we can get the date in a numpy array in a particular format i.e year-month-day by using numpy. datetime64() method. Syntax : numpy.datetime64(date) Return : Return the date in a format 'yyyy-mm-dd'.

What is timedelta64?

timedelta64() « Pandas date & time « Pandas « Numpy. Date and time calculations using Numpy timedelta64. Different units are used with timedelta64 for calculations, the list of units are given at the end of this tutorial. Let us create DataFrame with two datetime columns to calculate the difference.

How do you get all the dates corresponding to the month of July 2016 Numpy?

In NumPy to display all the dates for a particular month, we can do it with the help of NumPy. arrange() pass the first parameter the particular month and the second parameter the next month and the third parameter is the datatype datetime64[D]. It will return all the dates for the particular month.


1 Answers

It should work if you also specify a time unit parameter when creating the array. For example:

>>> t = np.empty(3, dtype='datetime64[s]')
>>> t
array(['1970-01-01T00:00:00+0000', '1970-01-01T00:00:00+0000',
       '1970-01-01T00:00:00+0000'], dtype='datetime64[s]')

And then you can also assign the values as required:

>>> t[0] = np.datetime64('2014-12-12 20:20:20')
>>> t
array(['2014-12-12T20:20:20+0000', '1970-01-01T00:00:00+0000',
       '1970-01-01T00:00:00+0000'], dtype='datetime64[s]')

NumPy doesn't permit datetimes with generic units (i.e. no units) to be represented. Creating the array t without the unit parameter and then trying to access the first element t[0] will raise this error:

ValueError: Cannot convert a NumPy datetime value other than NaT with generic units

Here, NumPy isn't able to infer what units the representation of the datetime should have. Guessing might lead to erroneous values given the varying lengths of calendar months and years.

This point isn't very explicit in the documentation but can be gleaned from the datetime page and is noted in the source code here.

like image 174
Alex Riley Avatar answered Sep 18 '22 08:09

Alex Riley