Converting pandas DatetimeIndex to 'float days format' with Matplotlib.dates.datestr2num

后端 未结 1 1168
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 17:36

Some Matplotlib methods need days in \'float days format\'. datestr2num is a converter function for this, but it falls over with the relevant pandas objects:



        
相关标签:
1条回答
  • 2020-12-11 18:10

    You can use the to_pydatetime method of the DatetimeIndex (this will convert it to an array of datetime.datetime's, and mpl.dates.date2num will know how to handle those):

    mpl.dates.date2num(df.index.to_pydatetime())
    

    The reason that date2num does not natively handle a pandas DatetimeIndex, is because matplotlib does not yet support the numpy datetime64 dtype (which is how the data are stored in a DatetimeIndex).

    0 讨论(0)
提交回复
热议问题