Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
THE ERROR I AM GETTING IS :-
Traceback (most recent call last):
  File "C:\Users\MAVERICK\Desktop\FINANCE PYTHON\candlestickgraph.py", line 16, in <module>
    ohlc = data.loc[:, ['Date', 'Open', 'High', 'Low', 'Close']]
  File "C:\Users\MAVERICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1762, in __getitem__
    return self._getitem_tuple(key)
  File "C:\Users\MAVERICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1289, in _getitem_tuple
    retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
  File "C:\Users\MAVERICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1954, in _getitem_axis
    return self._getitem_iterable(key, axis=axis)
  File "C:\Users\MAVERICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1595, in _getitem_iterable
    keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
  File "C:\Users\MAVERICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1552, in _get_listlike_indexer
    self._validate_read_indexer(
  File "C:\Users\MAVERICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1654, in _validate_read_indexer
    raise KeyError(
KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'



i was trying to get data directly to plot ohlc but its giving this error please help.
i have given the code which i tried:-

What I have tried:

# python_candlestick_chart.py
import datetime as dt
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import pandas as pd
import matplotlib.dates as mpl_dates
import pandas_datareader.data as web

plt.style.use('ggplot')

start = dt.datetime(1980,1,1)
end = dt.datetime(2020,5,22)

# Extracting Data for plotting
data = web.DataReader('INFY.NS' ,  'yahoo' , start , end)
ohlc = data.loc[:, ['Date', 'Open', 'High', 'Low', 'Close']]
ohlc['Date'] = pd.to_datetime(ohlc['Date'])
ohlc['Date'] = ohlc['Date'].apply(mpl_dates.date2num)
ohlc = ohlc.astype(float)

# Creating Subplots
fig, ax = plt.subplots()

candlestick_ohlc(ax, ohlc.values, width=0.6, colorup='green', colordown='red', alpha=0.8)

# Setting labels & titles
ax.set_xlabel('Date')
ax.set_ylabel('Price')
fig.suptitle('Daily Candlestick Chart of INFY')

# Formatting Date
date_format = mpl_dates.DateFormatter('%d-%m-%Y')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()

fig.tight_layout()

plt.show()
Posted
Updated 15-Aug-22 8:42am
Comments
Richard MacCutchan 3-Jul-20 11:04am    
KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'
PythonPreran 3-Jul-20 20:34pm    
i saw it sir but i am still not able to get it please tell me where is the problem ?
Richard MacCutchan 4-Jul-20 3:53am    
Sorry no idea. You need to study the documentation as the message states.
karma2447 6-Jul-20 0:54am    
Quote:In prior versions, using .loc[list-of-labels] would work as long as at least 1 of the keys was found (otherwise it would raise a KeyError). This behavior is deprecated and will show a warning message pointing to this section. The recommended alternative is to use .reindex().

Please refer : Indexing and selecting data — pandas 1.0.5 documentation[^]


Use labels, or reindex with labels.
What it means is, data.reindex(['Date', 'Open', 'High', 'Low', 'Close']])
Not 100% sure though, don't know what data looks like.
PythonPreran 6-Jul-20 1:28am    
I AM GETTING AN KEY ERROR saying 'Date'. Its not working.

The error seems pretty clear:
Quote:
KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'
Visit the link and follow the instructions:
Indexing and selecting data — pandas 1.0.5 documentation[^]
 
Share this answer
 
The reason is 'Date' is a index name, not a column label.

#solution: change index 'Date' to column 'Date', then can extract it.

df.reset_index(inplace=True)
ohlc = df.loc[:, ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]
 
Share this answer
 
Comments
Dave Kreskowiak 23-Mar-22 18:43pm    
i seriously doubt tHE op is still looking for an answer 2 years later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900