Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numpy.datetime64 to string object in python

I am having trouble converting a python datetime64 object into a string. For example:

t = numpy.datetime64('2012-06-30T20:00:00.000000000-0400') 

Into:

'2012.07.01' as a  string. (note time difference) 

I have already tried to convert the datetime64 object to a datetime long then to a string, but I seem to get this error:

dt = t.astype(datetime.datetime) #1341100800000000000L time.ctime(dt) ValueError: unconvertible time 
like image 263
cs_newbie Avatar asked Oct 21 '13 18:10

cs_newbie


People also ask

How do I convert datetime64 to string in Python?

You can use Numpy's datetime_as_string function. The unit='D' argument specifies the precision, in this case days. numpy instead of numpyp . Or with import numpy as np just np.

What is datetime64 in Numpy?

With the help of numpy. datetime64() method, we can get the date in a numpy array in a particular format i.e year-month-day by using numpy. datetime64() method. Syntax : numpy.datetime64(date) Return : Return the date in a format 'yyyy-mm-dd'.

Is datetime64 same as datetime?

NumPy's datetime64 and timedelta64 objectsNumPy has no separate date and time objects, just a single datetime64 object to represent a single moment in time. The datetime module's datetime object has microsecond precision (one-millionth of a second).


1 Answers

Solution was:

import pandas as pd  ts = pd.to_datetime(str(date))  d = ts.strftime('%Y.%m.%d') 
like image 86
cs_newbie Avatar answered Sep 27 '22 19:09

cs_newbie