Search code examples
python-3.xpandasdataframelogical-operators

Python OR Logic giving The truth value of a Series is ambiguous error


I am reading a CSV to Pandas DataFrame, counting the rows and then deleting all but the Column names and the first row of data (2 pairs of X,Y geographic coordinates). I subtract one from the other to get Delta_X and Delta_Y. I then want to see if these values are greater or less than Zero (0) so I can work out which Quadrant of a circle they are in. Based on the outcome I will do some calculations.

import pandas as pd
from pandas import DataFrame
import numpy as np
import csv
data= pd.read_csv('??????/11524_EQ20311_Lines.csv')

df=pd.DataFrame(data, columns=['SOLE','SOLN','EOLE','EOLN'])
numline=len(df.index) #Count the number of lines
df=df.iloc[:1] #Delete all but first line


Delta_X=df['EOLE']-df['SOLE']
Delta_Y=df['EOLN']-df['SOLN']

    Heading_Line=np.degrees(np.arctan((Delta_X)/(Delta_Y))) #Line heading
Length=((abs(df['SOLE']-df['EOLE']))**2+(abs(df['SOLN']-df['EOLN']))**2)**0.5   #Line Length
SOL_Orig_Distance=((2*BinX)**2+(0.5*BinY)**2)**0.5 #Distance diagonally from SOL to BG Origin
SOL_Orig_Angle=np.arctan((2*BinX)/(0.5*BinY))  #Angle from SOL to BG Origin when North up


if Delta_X>0 and Delta_Y>0:
    BGO_X=df['SOLE']-SOL_Orig_Distance*np.sin(SOL_Orig_Angle+Heading_Line)
    BGO_Y=df['SOLN']-SOL_Orig_Distance*np.cos(SOL_Orig_Angle+Heading_Line)
elif Delta_X>0 and Delta_Y<0:
    BGO_X=df['SOLE']-SOL_Orig_Distance*np.sin(SOL_Orig_Angle+(Heading_Line+np.pi))
    BGO_Y=df['SOLN']-SOL_Orig_Distance*np.cos(SOL_Orig_Angle+(Heading_Line+np.pi))
elif Delta_X<0 and Delta_Y<0:
    BGO_X=df['SOLE']-SOL_Orig_Distance*np.sin(SOL_Orig_Angle+(Heading_Line+np.pi))
    BGO_Y=df['SOLN']-SOL_Orig_Distance*np.cos(SOL_Orig_Angle+(Heading_Line+np.pi))
elif Delta_X<0 and Delta_Y>0:
    BGO_X=df['SOLE']-SOL_Orig_Distance*np.sin(SOL_Orig_Angle+(Heading_Line+2*np.pi))
    BGO_Y=df['SOLN']-SOL_Orig_Distance*np.cos(SOL_Orig_Angle+(Heading_Line+2*np.pi))

This is a excerpt of the CSV

Line #,Name,SOLE,SOLN,EOLE,EOLN,SOLKP,EOLKP
1,01001-04,433301.816,7182134.323,435354.908,7196488.236,0.000,14.500
2,01005-08,433326.564,7182130.783,435379.656,7196484.696,0.000,14.500
3,01009-12,433351.312,7182127.243,435404.404,7196481.156,0.000,14.500

I keep getting the same error no matter whether I use 'and' or '&' as suggested in some posts. I have also tried calling the SOLE/EOLE cells via df.loc and df.iloc. I even get the same error with just:

if Delta_X>0:

The error message is:

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

SOLE etc show as Float64. I have tried creating a variable named 'Zero',forcing it to Float then applying the logic to that i.e. Delta_X>Zero: but get the same error.


Solution

  • On reflection I do not need to perform this Quadrant calculation as working with radians negates the requirement.