I have a list of dates in string format like this:
dates=['2021-06-23','2021-06-22','2021-06-18']
I'm trying to gather data for a stock (cl=f//crude oil) using yfinance library. Here is the code uptil now:
I'm trying to return the data for dates in the dates(list).
import yfinance as yf
import pandas as pd
key='cl=f'
dates=['2021-06-23','2021-06-22','2021-06-18','2021-06-17']
data=yf.Ticker(key).history(period='max')
data['date']=data.index
data.reset_index(drop=True,inplace=True)
#rearranging columns
cols=['Open', 'High', 'Low', 'Close', 'Volume', 'Dividends', 'Stock Splits','date']
cols=cols[-1:]+cols[:-1]
data=data[cols]
print(data.head())
I've tried using:
data=data[(str(data['date'])== i for i in dates)]
It gave me the following error:
KeyError: "None of [Index([False, False, False, False], dtype='object')] are in the [columns]"
Why am I getting this error?
Some assistance required.