Python Forum
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() (/thread-29218.html)



ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Rejoice - Aug-23-2020

def rangetrendAnalysis(df1):
    if (df1['D1-OPEN'] > df1['D1-CLOSE']):
        return "Bearish"
    elif (df1['D1-OPEN'] < df1 ['D1-CLOSE']):
        return "Bullish"

nextDaydf['Result'] = rangetrendAnalysis(day1)
print (nextDaydf.head)
Error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Can someone shed some light on the mistake am making in here, am trying to pass a dataframe with multiple columns to a function which compare the values of two specific column ['D1-OPEN'] & [D1-CLOSE] and returned value is put in another dataframe (nextDaydf). Both the dataframes are of same index. however I am getting the error as above.


RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Larz60+ - Aug-23-2020

please post entire, unmodified error traceback. It contains valuable debugging information


RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Rejoice - Aug-24-2020

Error:
runfile('F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py', wdir='F:/RockzFX Academy 2020/Trading/Intraday_Analysis') Traceback (most recent call last): File "<ipython-input-8-c5b5b2e300c8>", line 1, in <module> runfile('F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py', wdir='F:/RockzFX Academy 2020/Trading/Intraday_Analysis') File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace) File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py", line 49, in <module> nextDaydf['Result'] = rangetrendAnalysis(day1) File "F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py", line 41, in rangetrendAnalysis if (df1['D1-OPEN'] > df1['D1-CLOSE']): File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__ .format(self.__class__.__name__)) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Error mentioned above is what I get, I get this even when I pass two dataframe into the function. I found a work around using condition, values and (np.select(conditions, values)). However I would still like to know why this is working and if there is any solution to it.


RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Larz60+ - Aug-24-2020

The initial error is on line 1 of Intraday_Analysis.py
Since you don't show that part of the code, I cannot guess why.


RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Rejoice - Aug-24-2020

import csv
import pandas as pd


indexdf = pd.read_excel("F:\RockzFX Academy 2020\Trading\Intraday_Analysis\Index.xlsx",header=None)
indexdf.set_index(indexdf.iloc[:,0],inplace=True,drop=False)

day1 = pd.read_csv('F:\RockzFX Academy 2020\Trading\Intraday_Analysis\MW-SECURITIES-IN-F&O-13-Aug-2020.csv')
day2 = pd.read_csv('F:\RockzFX Academy 2020\Trading\Intraday_Analysis\MW-SECURITIES-IN-F&O-14-Aug-2020.csv')

pd.set_option("display.max_rows", None, "display.max_columns", None)


# REMOVING SPACE & NEXT LINE CHARACTER FROM THE COLUMN
day1.rename(columns={'SYMBOL \n': 'SYMBOL', 'HIGH \n': 'D1-HIGH', 'LOW \n':'D1-LOW', 'LTP \n':'D1-CLOSE',  'OPEN \n':'D1-OPEN'}, inplace=True)
day2.rename(columns={'SYMBOL \n': 'SYMBOL', 'HIGH \n': 'D2-HIGH', 'LOW \n':'D2-LOW', 'LTP \n':'D2-CLOSE', 'OPEN \n':'D2-OPEN'}, inplace=True)

# TAKING ONLY SYMBOL, HIGH, LOW, & LTP = CLOSE FOR ANALYSIS
day1 = day1[['SYMBOL', 'D1-OPEN', 'D1-HIGH', 'D1-LOW', 'D1-CLOSE']]
day2 = day2[['SYMBOL', 'D2-OPEN', 'D2-HIGH', 'D2-LOW', 'D2-CLOSE']]

day1['SYMBOL']=day1['SYMBOL']+"-EQ"
day2['SYMBOL']=day2['SYMBOL']+"-EQ"

#CHANGING INDEX OF DATAFRAME TO SYMBOL
day1.set_index('SYMBOL', inplace=True, drop=False)
day2.set_index('SYMBOL', inplace=True, drop=False)

day1=day1.reindex(indexdf.index)
day2=day2.reindex(indexdf.index)

#Removing "," from the string
day1 = day1.replace(',','', regex=True)
day2 = day2.replace(',','', regex=True)


def rangetrendAnalysis(df1):
    if (df1['D1-OPEN'] > df1['D1-CLOSE']):
        return "Bearish"
    elif (df1['D1-OPEN'] < df1 ['D1-CLOSE']):
        return "Bullish"

 
nextDaydf['Result'] = rangetrendAnalysis(day1)
print (nextDaydf.head)
Error:
runfile('F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py', wdir='F:/RockzFX Academy 2020/Trading/Intraday_Analysis') Traceback (most recent call last): File "<ipython-input-6-c5b5b2e300c8>", line 1, in <module> runfile('F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py', wdir='F:/RockzFX Academy 2020/Trading/Intraday_Analysis') File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace) File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py", line 52, in <module> nextDaydf['Result'] = rangetrendAnalysis(day1) File "F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py", line 46, in rangetrendAnalysis if (df1['D1-OPEN'] > df1['D1-CLOSE']): File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__ .format(self.__class__.__name__)) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().



RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Larz60+ - Aug-25-2020

This is odd.
You are using pandas so I don't think you need the import csv.
Try removing that.


RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Rejoice - Aug-25-2020

I removed it and still getting the same error

Error:
runfile('F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py', wdir='F:/RockzFX Academy 2020/Trading/Intraday_Analysis') Traceback (most recent call last): File "<ipython-input-1-c5b5b2e300c8>", line 1, in <module> runfile('F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py', wdir='F:/RockzFX Academy 2020/Trading/Intraday_Analysis') File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace) File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py", line 52, in <module> nextDaydf['Result'] = rangetrendAnalysis(day1) File "F:/RockzFX Academy 2020/Trading/Intraday_Analysis/topost.py", line 46, in rangetrendAnalysis if (df1['D1-OPEN'] > df1['D1-CLOSE']): File "C:\Users\REJO-HOME\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__ .format(self.__class__.__name__)) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().