Python Forum
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item()
#1
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.
Reply
#2
please post entire, unmodified error traceback. It contains valuable debugging information
Reply
#3
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.
Reply
#4
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.
Reply
#5
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().
Reply
#6
This is odd.
You are using pandas so I don't think you need the import csv.
Try removing that.
Reply
#7
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().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,644 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Bool Object is not Subscriptable quest 1 4,136 May-02-2021, 11:12 AM
Last Post: Yoriz
  Python ValueError The value of a Series is abmbiguous nhl66pens 2 2,221 Oct-17-2020, 02:46 PM
Last Post: jefsummers
  bool b = (num == 100) this doesn't work? MelonMusk 2 1,986 Jun-12-2020, 02:18 AM
Last Post: bowlofred
  Convert quarterly time series to monthly time series donnertrud 1 5,149 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() bongielondy 2 14,121 Nov-27-2019, 03:25 PM
Last Post: ThomasL
  ValueError - arg is an empty sequence jojotte 2 5,360 Dec-31-2018, 10:07 AM
Last Post: jojotte
  bool PreservedKillich 6 3,249 Aug-01-2018, 03:09 AM
Last Post: Skaperen
  [split] Set bool variable RedSkeleton007 1 2,711 Oct-22-2017, 09:28 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020