Wednesday, 26 December 2018

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

I'm trying to draw a timeline

import datetime          as da
import matplotlib.dates  as dt

# Data
df = pd.DataFrame({'A': [da.datetime(2017,1,5,9,8),   da.datetime(2017,1,5,9,9),  da.datetime(2017,1,7,9,19), da.datetime(2017,1,7,9,19),  da.datetime(2017,1,7,9,19), da.datetime(2017,2,7,9,19), da.datetime(2017,2,7,9,19)],
                   'B': [da.datetime(2017,1,5,9,9),   da.datetime(2017,1,5,9,12), da.datetime(2017,1,7,9,26), da.datetime(2017,1,7,9,20),  da.datetime(2017,1,7,9,21), da.datetime(2017,2,7,9,23), da.datetime(2017,2,7,9,25)],
                   'C' :[1,                           2,                          3,                          4,                           5,                          6,                          7 ]})

# Visualisation
ax = plt.subplot()
ax = plt.hlines(df.C,
    dt.date2num(df.A),
    dt.date2num(df.B))

but getting the error:

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

I think it's caused by the data type:

df.A.dtype 
dtype('<M8[ns]')

I tried some recommended solutions (converter & pandacnv) but I still couldn't get it to work.



from AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'

No comments:

Post a Comment