Code Error

Hello ,

 Can you please help me understand why this code is generating this error? 

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_25208\1532132358.py in ?()
---> 55 import yfinance as yf
     56 import pandas as pd
     57 from ta.trend import SMAIndicator
     58 from ta.momentum import RSIIndicator

~\AppData\Local\Temp\ipykernel_25208\1532132358.py in ?(data)
     13 def generate_signals(data):
     14     window = 20
     15     sma_indicator = SMAIndicator(data['Close'], window=window)
---> 16     data['SMA20'] = sma_indicator.sma_indicator()
     17     data['Higher_Low1'] = data['Low'] > data['Low'].shift(1)
     18     data['Higher_Low2'] = data['Low'].shift(1) > data['Low'].shift(2)
     19     data['Close_Above_Prior_Close'] = data['Close'] > data['Close'].shift(1)

D:\Anaconda\envs\quantra_py\lib\site-packages\ta\trend.py in ?(self)
    192         Returns:
    193             pandas.Series: New feature generated.
    194         """
    195         sma_ = _sma(self._close, self._window, self._fillna)
--> 196         return pd.Series(sma_, name=f"sma_{self._window}")

D:\Anaconda\envs\quantra_py\lib\site-packages\pandas\core\series.py in ?(self, data, index, dtype, name, copy, fastpath)
    471                 data = data.reindex(index, copy=copy)
    472                 copy = False
    473                 data = data._mgr
    474         elif is_dict_like(data):
--> 475             data, index = self._init_dict(data, index, dtype)
    476             dtype = None
    477             copy = False
    478         elif isinstance(data, (SingleBlockManager, SingleArrayManager)):

D:\Anaconda\envs\quantra_py\lib\site-packages\pandas\core\series.py in ?(self, data, index, dtype)
    545         keys: Index | tuple
    546 
    547         # Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')]
    548         # raises KeyError), so we iterate the entire dict, and align
--> 549         if data:
    550             # GH:34717, issue was using zip to extract key and values from data.
    551             # using generators in effects the performance.
    552             # Below is the new way of extracting the keys and values

D:\Anaconda\envs\quantra_py\lib\site-packages\pandas\core\generic.py in ?(self)
   1517     @final
   1518     def __nonzero__(self) -> NoReturn:
-> 1519         raise ValueError(
   1520             f"The truth value of a {type(self).__name__} is ambiguous. "
   1521             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1522         )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Hi Pierre,



This usually happens when the code encounters an empty value and so cannot move further. This could be due to various reasons like the data contains empty values itself. Another reason could be that the code is not written correctly.



Just to remove possibilities, you can check the syntax of the method in the ta library, which I believe is the technical analysis library.

You can also drop any empty values using the '.dropna' function. 



Hope this helps.