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')

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')

This is the simplest way:

t.item().strftime('%Y.%m.%d')

item() gives you a Python native datetime object, on which all the usual methods are available.

Tags:

Python

Numpy