I have a numpy array which is composed of numpy.datetime64 values. I'd like to convert these to pandas Timestamps using pandas.Timestamp().
I could do an explicit for-loop like
import numpy as np
import pandas as pd
stamps = [pd.Timestamp(t) for t in my_arr]
but this isn't very efficient. I can use numpy's vectorize function to do something like this instead
stamper = np.vectorize(pd.Timestamp)
stamps = stamper(my_arr)
but the numpy documentation states that vectorize is mostly a convenience function and not intended for performance. Is there a better, more efficient way to do this?
Edit: Here are some timings from some of the solutions given:
%timeit stamper(my_arr)
%timeit my_arr.astype(pd.Timestamp)
%timeit np.array([pd.Timestamp(t) for t in my_arr])
%timeit pd.to_datetime(my_arr)
100 loops, best of 3: 7.04 ms per loop
10000 loops, best of 3: 82 µs per loop
100 loops, best of 3: 16.8 ms per loop
1000 loops, best of 3: 1.19 ms per loop
Seems that the .astype() is fastest, so I'll go with this. Thanks!
pd.DataFrame(my_arr).to_timestamp()do what you want?pd.DataFrame(my_arr).to_timestamp(axis=1)