0

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

FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
  • Is that the entire error output? Have you done any debugging? I would recommend reading https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – AMC May 10 '21 at 14:43

1 Answers1

0

Solution 1: Replace close_price[tickers] = get_history(tickers,start,end) with close_price[tickers] = get_history(tickers,start,end)['Close'].


Solution 2: Replace close_price = pd.DataFrame() with close_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

close_price[tickers] = get_history(tickers,start,end)

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).