简体   繁体   中英

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

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?

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.

I prefer this workaround because sometimes np.datetime64 has different resolution

def ___convert_to_datetime(d):
    return datetime.strptime(np.datetime_as_string(d,unit='s'), '%Y-%m-%dT%H:%M:%S')

for timestamp

def ___convert_to_ts(d):
    return datetime.strptime(np.datetime_as_string(d,unit='s'), '%Y-%m-%dT%H:%M:%S').timestamp()

for instance

import numpy as np
from datetime import datetime


def ___convert_to_datetime(d):
  return datetime.strptime(np.datetime_as_string(d,unit='s'), '%Y-%m-%dT%H:%M:%S')



def ___convert_to_ts(d):
  return datetime.strptime(np.datetime_as_string(d,unit='s'), '%Y-%m-%dT%H:%M:%S').timestamp()


print(___convert_to_datetime(np.datetime64('2005-02-25')))

my_ns_date = np.datetime64('2009') + np.timedelta64(20, 'ns')

print(my_ns_date)

print(___convert_to_datetime(my_ns_date))

output will be

2005-02-25 00:00:00

2009-01-01T00:00:00.000000020

2009-01-01 00:00:00

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

try this code

to convert numpy datetime64[ns] to python datetime you just try the following code segment

from datetime import datetime
datetime.utcfromtimestamp('your_time_stamp')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM