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()
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
[
^
]
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']]
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
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.