Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make candle chart due to some error with mpf.plot

import pandas as pd
import yfinance as yf
import mplfinance as mpf

df = yf.download('AMZN', start='2020-01-01', end='2025-07-31')
print(df)
mpf.plot(df['2020-01-01':'2020-06-01'], type='candle', volume=True)

This is the code I have written.

This is the output:

Price            Close        High         Low        Open    Volume
Ticker            AMZN        AMZN        AMZN        AMZN      AMZN
Date
2020-01-02   94.900497   94.900497   93.207497   93.750000  80580000
2020-01-03   93.748497   94.309998   93.224998   93.224998  75288000
2020-01-06   95.143997   95.184502   93.000000   93.000000  81236000
2020-01-07   95.343002   95.694504   94.601997   95.224998  80898000
2020-01-08   94.598503   95.550003   94.321999   94.902000  70160000
...                ...         ...         ...         ...       ...
2025-07-14  225.690002  226.660004  224.240005  225.070007  35702600
2025-07-15  226.350006  227.270004  225.460007  226.199997  34907300
2025-07-16  223.190002  226.100006  222.179993  225.880005  39535900
2025-07-17  223.880005  224.500000  222.509995  223.320007  31855800
2025-07-18  226.130005  226.399994  222.979996  225.139999  37811600

[1393 rows x 5 columns]

This is the error:

    mpf.plot(df['2020-01-01':'2020-06-01'], type='candle', volume=True)
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
                                            ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
raise ValueError('Column "'+col+'" NOT FOUND in Input DataFrame!'+
                     '\n            CHECK that your column names are correct AND/OR'+
                     '\n            CHECK for leading or trailing blanks in your column names.')
ValueError: Column "Open" NOT FOUND in Input DataFrame!
            CHECK that your column names are correct AND/OR

I tried to make candle stick chart using mplfinance but I could not. It seems that there is some error with the way I use the plot() function but I tried checking the syntax and as per my observations, it is correct.

like image 840
rashmip_21 Avatar asked Oct 24 '25 03:10

rashmip_21


1 Answers

It appears that, on or about Oct 21, 2024, with version 0.2.46, yf.download started returning, by default, a MultiIndex for the columns.

You can fix this by setting multi_level_index=False in your call to yf.download. This should work:

import pandas as pd
import yfinance as yf
import mplfinance as mpf

df = yf.download('AMZN', start='2020-01-01', end='2025-07-31', multi_level_index=False)
print(df)
mpf.plot(df['2020-01-01':'2020-06-01'], type='candle', volume=True)

How could you have found this?

  1. Note carefully the error message:
ValueError: Column "Open" NOT FOUND in Input DataFrame!
CHECK that your column names are correct AND/OR
CHECK for leading or trailing blanks in your column names.
  1. To check your column names:
print("df.columns =",df.columns)
  1. The output would be:
df.columns= MultiIndex([( 'Close', 'AMZN'),
            (  'High', 'AMZN'),
            (   'Low', 'AMZN'),
            (  'Open', 'AMZN'),
            ('Volume', 'AMZN')],
           names=['Price', 'Ticker'])

which clearly shows what the error message is saying, Column "Open" NOT FOUND in Input DataFrame!

hth


I suspect that the reason yf.download() defaulted to MultiIndex is because you can, in theory, ask for multiple Tickers at the same time, and a MultiIndex will keep them separate. Personally I would prefer it to count the number of tickers and not use a MultiIndex unless I ask for more than one ticker. However possibly the authors did not want yf.download to vary it's return type based on the input (certainly a valid argument) and so they chose to always return MultiIndex.

By the way, on a separate note: I am also seeing the following message from yf when I run it: YF.download() has changed argument auto_adjust default to True so if you don't want auto_adjust you may want to also set that to False. Your choice.

Full Disclosure: I am the maintainer of the mplfinance package.

like image 86
Daniel Goldfarb Avatar answered Oct 25 '25 19:10

Daniel Goldfarb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!