Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert numpy datetime64 [ns] to python datetime?

Tags:

python

numpy

I need to convert dates from pandas frame values in the separate function:

 def myfunc(lat, lon, when):
        ts = (when - np.datetime64('1970-01-01T00:00:00Z','s')) / np.timedelta64(1, 's')
        date = datetime.datetime.utcfromtimestamp(ts)
        print("Numpy date= ", when, " Python date= ", date)
        return float(90) - next_func(lat, lon, date)

Invokation this function:

new_df['new_column'] =  np.vectorize(my_func)(lat, lon, new_df['datetime(LT)'])  

But it raise error:

ufunc subtract cannot use operands with types dtype('int64') and dtype('<M8[s]')

How to convert numpy datetime64 [ns] to python datetime?

like image 869
Boris Salimov Avatar asked Jan 01 '23 16:01

Boris Salimov


1 Answers

I wonder if you need all this conversion work. With the right time units a datetime64 can produce a datetime object directly.

I'm not sure about your when variable, but let's assume it comes from pandas, and is something like a DatetimeIndex:

In [56]: time = pandas.date_range('6/28/2013', periods=5, freq='5D')
In [57]: time
Out[57]: 
DatetimeIndex(['2013-06-28', '2013-07-03', '2013-07-08', '2013-07-13',
               '2013-07-18'],
              dtype='datetime64[ns]', freq='5D')

The equivalent numpy array

In [58]: time.values
Out[58]: 
array(['2013-06-28T00:00:00.000000000', '2013-07-03T00:00:00.000000000',
       '2013-07-08T00:00:00.000000000', '2013-07-13T00:00:00.000000000',
       '2013-07-18T00:00:00.000000000'], dtype='datetime64[ns]')
In [59]: time.values.tolist()
Out[59]: 
[1372377600000000000,
 1372809600000000000,
 1373241600000000000,
 1373673600000000000,
 1374105600000000000]

With [ns] the result is a large integer, a 'timestamp' of some sort. But if I convert the time units to something like seconds, or even microseconds (us):

In [60]: time.values.astype('datetime64[s]')
Out[60]: 
array(['2013-06-28T00:00:00', '2013-07-03T00:00:00',
       '2013-07-08T00:00:00', '2013-07-13T00:00:00',
       '2013-07-18T00:00:00'], dtype='datetime64[s]')
In [61]: time.values.astype('datetime64[s]').tolist()
Out[61]: 
[datetime.datetime(2013, 6, 28, 0, 0),
 datetime.datetime(2013, 7, 3, 0, 0),
 datetime.datetime(2013, 7, 8, 0, 0),
 datetime.datetime(2013, 7, 13, 0, 0),
 datetime.datetime(2013, 7, 18, 0, 0)]

the result is a list of datetime objects.

like image 197
hpaulj Avatar answered Jan 13 '23 21:01

hpaulj