Convert Numpy.Datetime64 to String Object in Python

Pandas datetime64 to string

You can just cast the dtype first using astype:

In[29]:
df = pd.DataFrame({'date':pd.to_datetime(['2018-05-20 10:20:00','2018-05-20 10:30:00'])})
df

Out[29]:
date
0 2018-05-20 10:20:00
1 2018-05-20 10:30:00

In[30]:
df['date'].astype(str).tolist()

Out[30]: ['2018-05-20 10:20:00', '2018-05-20 10:30:00']

What you did just converted the array to a list of the original dtype:

In[31]:
df['date'].tolist()

Out[31]: [Timestamp('2018-05-20 10:20:00'), Timestamp('2018-05-20 10:30:00')]

The more formal method is to call dt.strftime to convert to string using the passed in format:

In[33]:
df['date'].dt.strftime('%Y-%m-%d %H:%M:%S').tolist()

Out[33]: ['2018-05-20 10:20:00', '2018-05-20 10:30:00']

Can't call strftime on numpy.datetime64, no definition

Use this code:

import pandas as pd 
t= pd.to_datetime(str(date))
timestring = t.strftime('%Y.%m.%d')

numpy.array.tolist() converts numpy.datetime64 to int

Explicitly casting the numpy.ndarray as a native Python list will preserve the contents as numpy.datetime64 objects:

>>> list(my_array)
[numpy.datetime64('2017-06-28T22:47:51.213500000'),
numpy.datetime64('2017-06-28T22:48:37.570900000'),
numpy.datetime64('2017-06-28T22:49:46.736800000'),
numpy.datetime64('2017-06-28T22:50:41.866800000'),
numpy.datetime64('2017-06-28T22:51:17.024100000'),
numpy.datetime64('2017-06-28T22:51:24.038300000')]

However, if you wanted to go back from an integer timestamp to a numpy.datetime64 object, the number given here by numpy.ndarray.tolist is given in nanosecond format, so you could also use a list comprehension like the following:

>>> [np.datetime64(x, "ns") for x in my_list]
[numpy.datetime64('2017-06-28T22:47:51.213500000'),
numpy.datetime64('2017-06-28T22:48:37.570900000'),
numpy.datetime64('2017-06-28T22:49:46.736800000'),
numpy.datetime64('2017-06-28T22:50:41.866800000'),
numpy.datetime64('2017-06-28T22:51:17.024100000'),
numpy.datetime64('2017-06-28T22:51:24.038300000')]

And if you want the final result as a Python datetime.datetime object instead of a numpy.datetime64 object, you can use a method like this (adjusted as needed for locality):

>>> from datetime import datetime
>>> list(map(datetime.utcfromtimestamp, my_array.astype(np.uint64) / 1e9))
[datetime.datetime(2017, 6, 28, 22, 47, 51, 213500),
datetime.datetime(2017, 6, 28, 22, 48, 37, 570900),
datetime.datetime(2017, 6, 28, 22, 49, 46, 736800),
datetime.datetime(2017, 6, 28, 22, 50, 41, 866800),
datetime.datetime(2017, 6, 28, 22, 51, 17, 24100),
datetime.datetime(2017, 6, 28, 22, 51, 24, 38300)]

Edit: Warren Weckesser's answer provides a more straightforward approach to go from a numpy.datetime64[ns] array to a list of Python datetime.datetime objects than is described here.

How to Convert a List to numpy.datetime64 format

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?

Convert string to NumPy datetime64 dtype

pd.to_datetime

import pandas as pd

pd.to_datetime(date, format='%Y%m%d%H%M')
Timestamp('2017-11-08 17:50:00')

The important bit here is the format string '%Y%m%d%H%M'.


datetime.datetime equivalent in python.

from datetime import datetime as dt

dt.strptime(date, '%Y%m%d%H%M')
datetime.datetime(2017, 11, 8, 17, 50)

Numpy datetime64 time units - conversion from string with more than 4 year digits

Because a np.datetime64[ns] is a 64 bits integer number of nano seconds since 1970-01-01 00:00, so they can only represent dates in the [1678 AD, 2262 AD] range. All timestamps outside that range are folded into it.

Reference : numpy manual on Datetimes

Converting between datetime, Timestamp and datetime64

To convert numpy.datetime64 to datetime object that represents time in UTC on numpy-1.8:

>>> from datetime import datetime
>>> import numpy as np
>>> dt = datetime.utcnow()
>>> dt
datetime.datetime(2012, 12, 4, 19, 51, 25, 362455)
>>> dt64 = np.datetime64(dt)
>>> ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
>>> ts
1354650685.3624549
>>> datetime.utcfromtimestamp(ts)
datetime.datetime(2012, 12, 4, 19, 51, 25, 362455)
>>> np.__version__
'1.8.0.dev-7b75899'

The above example assumes that a naive datetime object is interpreted by np.datetime64 as time in UTC.


To convert datetime to np.datetime64 and back (numpy-1.6):

>>> np.datetime64(datetime.utcnow()).astype(datetime)
datetime.datetime(2012, 12, 4, 13, 34, 52, 827542)

It works both on a single np.datetime64 object and a numpy array of np.datetime64.

Think of np.datetime64 the same way you would about np.int8, np.int16, etc and apply the same methods to convert between Python objects such as int, datetime and corresponding numpy objects.

Your "nasty example" works correctly:

>>> from datetime import datetime
>>> import numpy
>>> numpy.datetime64('2002-06-28T01:00:00.000000000+0100').astype(datetime)
datetime.datetime(2002, 6, 28, 0, 0)
>>> numpy.__version__
'1.6.2' # current version available via pip install numpy

I can reproduce the long value on numpy-1.8.0 installed as:

pip install git+https://github.com/numpy/numpy.git#egg=numpy-dev

The same example:

>>> from datetime import datetime
>>> import numpy
>>> numpy.datetime64('2002-06-28T01:00:00.000000000+0100').astype(datetime)
1025222400000000000L
>>> numpy.__version__
'1.8.0.dev-7b75899'

It returns long because for numpy.datetime64 type .astype(datetime) is equivalent to .astype(object) that returns Python integer (long) on numpy-1.8.

To get datetime object you could:

>>> dt64.dtype
dtype('<M8[ns]')
>>> ns = 1e-9 # number of seconds in a nanosecond
>>> datetime.utcfromtimestamp(dt64.astype(int) * ns)
datetime.datetime(2002, 6, 28, 0, 0)

To get datetime64 that uses seconds directly:

>>> dt64 = numpy.datetime64('2002-06-28T01:00:00.000000000+0100', 's')
>>> dt64.dtype
dtype('<M8[s]')
>>> datetime.utcfromtimestamp(dt64.astype(int))
datetime.datetime(2002, 6, 28, 0, 0)

The numpy docs say that the datetime API is experimental and may change in future numpy versions.



Related Topics



Leave a reply



Submit