Code:
import numpy as np
import pandas as pd
from datetime import datetime,date,time,timedelta
from nsepy import get_history
stocks = ['JSWSTEEL','RELIANCE','AXISBANK','HCLTECH','TECHM']
start = datetime.today() - timedelta(365)
end = datetime.today()
close_price = pd.DataFrame()
for tickers in stocks:
close_price[tickers] = get_history(tickers,start,end)
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series
kindly help me in above code to get information for multiple stocks
Thanks
Solution 1: Replace
close_price[tickers] = get_history(tickers,start,end)withclose_price[tickers] = get_history(tickers,start,end)['Close'].Solution 2: Replace
close_price = pd.DataFrame()withclose_price = {}.Explanation:
This error is coming because you have defined 'close_price' as a Dataframe which is not the case. As the get_history function, itself returns a pandas DataFrame so in the below line
you are actually assigning a data frame to a column of close_price. As 2-D dataframe (returned by get_history) can not be converted into a Series, hence the error.
So you can simply define close_price as a dictionary instead of dataframe
or
extract Close price series from the returned dataframe, extract close price from it and assign it to your defined dataframe ( which is what you are trying to do).