pythonnumpydatetime64

How to Convert a List to numpy.datetime64 format


I know that we can create a single string to np.datetime64 format such as:

a = np.datetime64('2020-01-01')

But what if we have a list with multiple strings of dates in it?

How are we able to apply the same np.datetime64 to convert all the elements inside into a datetime format? Apart from doing a for-loop perhaps.


Solution

  • Using list comprehension:

    strings_list= [...]
    npdate_list = [np.datetime64(x) for x in strings_list]
    

    Is there a specific reason for you to want to avoid a loop?

    List comprehension is okay?