0

I have a panda dataframe with the following columns: enter image description here

Now I would like to plot out a chart with mpf.plot. But I am getting the following error: enter image description here

How can I avoid the above TypeError Message? I did set the df index with the following command:

dfnew_plot = dfnew_plot.set_index('Date')
Yvar
  • 155
  • 4
  • 14
  • Are you sure Date is already a datetime column? in other words, when you print ```type(dfnew_plot.index)``` does it return ```pandas.core.indexes.datetimes.DatetimeIndex``` ? – solopiu Jan 30 '21 at 11:04
  • Result is pandas.core.indexes.base.Index – Yvar Jan 30 '21 at 11:48

1 Answers1

2

Your index isn't the correct dtype. It needs to be converted using:

dfnew_plot.index = pd.to_datetime(dfnew_plot.index)
Ollie in PGH
  • 2,559
  • 2
  • 16
  • 19
  • Thanks a lot for your help. Your solution solves my problem. Question: If I print the df before and after the code dfnew_plot.index = pd.to_datetime(dfnew_plot.index), i do not see any differences? What do I have to learn, to understand the difference? – Yvar Jan 30 '21 at 11:41
  • Your index was a dtype of string or "object" according to pandas. To check the dtype of any Series (column, or index) you can use the dtype attribute. Example: `dfnew_plot.index.dtype'. If the result is "O" or "Object" or anything besides "DateTimeIndex" then your financial plotting library won't work. – Ollie in PGH Jan 30 '21 at 11:57