Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

I have a datetime64 t that I'd like to represent as a string.

When I call strftime like this t.strftime('%Y.%m.%d') I get this error:

AttributeError: 'numpy.datetime64' object has no attribute 'strftime' 

What am I missing? I am using Python 3.4.2 and Numpy 1.9.1

like image 870
deeb Avatar asked Feb 04 '15 16:02

deeb


2 Answers

Importing a data structures library like pandas to accomplish type conversion feels like overkill to me. You can achieve the same thing with the standard datetime module:

import numpy as np import datetime t = np.datetime64('2017-10-26') t = t.astype(datetime.datetime) timestring = t.strftime('%Y.%m.%d') 
like image 103
apteryx Avatar answered Sep 16 '22 12:09

apteryx


Use this code:

import pandas as pd  t= pd.to_datetime(str(date))  timestring = t.strftime('%Y.%m.%d') 
like image 29
user 12321 Avatar answered Sep 16 '22 12:09

user 12321