Python Forum
conditions not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conditions not working as expected
#1
I am pulling/wanting to pull in data from a pandas data frame to a straight variable so I can do pull it into a function so I do not duplicate code. When I do it it is not hitting the conditions as expected.

7651/7651b.csv
key,address,accountnumber,opendataupdate,housetype,landuse,exemptclass,assessmentyear,currentyeartotalassessment,owneroccupancycode,homesteadcreditqualificationcode,homesteadqualificationdate,yearbuilt,datepurchased,zoning,box2,box4,box5,box6,box8,box7,box9a,box9,box10,totalchange
79790234333,7651 TIMBERCROSS LN,79790234333,20230303,TH,Residential (R),Blank,2023,310500,Yes,Approved,2015.01.14,2014,2014.07.31,,310500,290000,120000,231500,310500,351500,20500.0,331000.0,351500,61500
7651.py
# !/usr/bin/env python
author = "Michael Brown"

import pandas as pd
from limits import statelimit, annearundelcountylimit
from rates import statetaxrate, annearundeltaxrate, annearundelsolidwaste, annearundelstormwater
from warnings import simplefilter

simplefilter(action="ignore", category=pd.errors.PerformanceWarning)

tanyardTH = pd.read_csv('test/7651b.csv')

# year 1 calculation
tanyardTH['year1difference'] = tanyardTH['box8'] - tanyardTH['box4']
tanyardTH['year1countylimit'] = tanyardTH['box4'] + (tanyardTH['box4'] * annearundelcountylimit)
tanyardTH['year1statelimit'] = tanyardTH['box4'] + (tanyardTH['box4'] * statelimit)
tanyardTH['year1countydifference'] = tanyardTH['box8'] - tanyardTH['year1countylimit']
tanyardTH['year1statedifference'] = tanyardTH['box8'] - tanyardTH['year1statelimit']

# year1 county credit calculation
tanyardTH.loc[tanyardTH['year1countydifference'] < 0, 'year1countycredit'] = 0
tanyardTH.loc[tanyardTH['year1countydifference'] > 0, 'year1countycredit'] = (tanyardTH[
                                                                                  'year1countydifference'] * annearundeltaxrate) / 100

# year 1 state credit calculation
tanyardTH.loc[tanyardTH['year1statedifference'] < 0, 'year1statecredit'] = 0
tanyardTH.loc[tanyardTH['year1statedifference'] > 0, 'year1statecredit'] = (tanyardTH[
                                                                                'year1statedifference'] * statetaxrate) / 100

# year 1 straight real estate tax payment without exempt class
tanyardTH['year1countyrealestate'] = (tanyardTH['box8'] * annearundeltaxrate) / 100
tanyardTH['year1staterealestate'] = (tanyardTH['box8'] * statetaxrate) / 100
tanyardTH['year1total'] = tanyardTH['year1countyrealestate'] + tanyardTH['year1staterealestate'] - tanyardTH[
    'year1countycredit'] - tanyardTH['year1statecredit'] + annearundelsolidwaste + annearundelstormwater

# year 2 calculation
tanyardTH['year2countylimit'] = tanyardTH['year1countylimit'] + (tanyardTH['year1countylimit'] * annearundelcountylimit)
tanyardTH['year2statelmit'] = tanyardTH['box2'] + (tanyardTH['box2'] * statelimit)
tanyardTH['year2countydifference'] = tanyardTH['box9'] - tanyardTH['year2countylimit']
tanyardTH['year2statedifference'] = tanyardTH['box8'] - tanyardTH['year2statelmit']

# year 2 county credit calculation
tanyardTH.loc[tanyardTH['year2countydifference'] < 0, 'year2countycredit'] = 0
tanyardTH.loc[tanyardTH['year2countydifference'] > 0, 'year2countycredit'] = (tanyardTH[
                                                                                  'year2countydifference'] * annearundeltaxrate) / 100

# year 2 state credit calculation
tanyardTH.loc[tanyardTH['year2statedifference'] < 0, 'year2statecredit'] = 0
tanyardTH.loc[tanyardTH['year2statedifference'] > 0, 'year2statecredit'] = (tanyardTH[
                                                                                'year2statedifference'] * statetaxrate) / 100

# year 2 straight real estate tax payment without exempt class
tanyardTH['year2countyrealestate'] = (tanyardTH['box9'] * annearundeltaxrate) / 100
tanyardTH['year2staterealestate'] = (tanyardTH['box9'] * statetaxrate) / 100
tanyardTH['year2total'] = tanyardTH['year2countyrealestate'] + tanyardTH['year2staterealestate'] - tanyardTH[
    'year2countycredit'] - tanyardTH['year2statecredit'] + annearundelsolidwaste + annearundelstormwater

# year 3 calculation
tanyardTH['year3countylimit'] = tanyardTH['year2countylimit'] + (tanyardTH['year2countylimit'] * annearundelcountylimit)
tanyardTH['year3statelimit'] = tanyardTH['box9'] + (tanyardTH['box9'] * statelimit)
tanyardTH['year3countydifference'] = tanyardTH['box10'] - tanyardTH['year3countylimit']
tanyardTH['year3statedifference'] = tanyardTH['box9'] - tanyardTH['year3statelimit']

# year 3 county credit calculation
tanyardTH.loc[tanyardTH['year3countydifference'] < 0, 'year3countycredit'] = 0
tanyardTH.loc[tanyardTH['year3countydifference'] > 0, 'year3countycredit'] = (tanyardTH[ 'year3countydifference'] * annearundeltaxrate) / 100

# year 3 state credit calculation
tanyardTH.loc[tanyardTH['year3statedifference'] < 0, 'year3statecredit'] = 0
tanyardTH.loc[tanyardTH['year3statedifference'] > 0, 'year3statecredit'] = (tanyardTH['year3statedifference'] * statetaxrate) / 100

# year3 straight real estate tax payment without exempt class
tanyardTH['year3countyrealestate'] = (tanyardTH['box10'] * annearundeltaxrate) / 100
tanyardTH['year3staterealestate'] = (tanyardTH['box10'] * statetaxrate) / 100

tanyardTH.loc[(tanyardTH['owneroccupancycode'] == 'Yes') & (tanyardTH['homesteadcreditqualificationcode'] == 'Approved'), 'year3total']\
    = (tanyardTH['year3countyrealestate'] + tanyardTH['year3staterealestate'] - tanyardTH['year3countycredit'] - tanyardTH['year3statecredit'] + annearundelsolidwaste + annearundelstormwater)
tanyardTH.loc[(tanyardTH['owneroccupancycode'] != 'Yes') | (tanyardTH['homesteadcreditqualificationcode'] != 'Approved'), 'year3total'] = (tanyardTH['year3countyrealestate'] + tanyardTH['year3staterealestate'] + annearundelsolidwaste + annearundelstormwater)

from functions import test

tanyardTH[["exemptclass"]].to_numpy()

test = test(tanyardTH['owneroccupancycode'],tanyardTH['homesteadcreditqualificationcode'], tanyardTH['exemptclass'], tanyardTH['year3countyrealestate'], tanyardTH['year3staterealestate'], tanyardTH['year3countycredit'], tanyardTH['year3statecredit'])
#functions can only have 2 arguments?

# debugging
print(tanyardTH)
tanyardTH.to_csv('7651.csv')
print(tanyardTH.info())
functions.py
def owneroccupancycondition(x):
    if x == "H":
        return "Yes"
    elif x == "N":
        return " No "
    else:
        return "0"


def yearcondition(x):
    if x == "O":
        return "1899"
    else:
        return x


def homesteadqualiticationcondition(x):
    if x == "A":
        return "Approved"
    if x == "X":
        return "Denied"
    else:
        return "No Application"

def test(owneroccupied, homesteadcode, exemptclass, year3countyrealestate, year3staterealestate, year3countycredit, year3statecredit):
    from rates import statetaxrate, annearundeltaxrate, annearundelsolidwaste, annearundelstormwater

    if ("exemptclass" != "Blank"):
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill
    if ("exemptclass" == "Blank"):
        if ("owneroccupied" == "Yes"):
            taxbill = year3countyrealestate + year3staterealestate - year3countycredit - year3statecredit + annearundelsolidwaste + annearundelstormwater
            return taxbill
        else:
            taxbill = year3countyrealestate + year3staterealestate + annearundelsolidwaste + annearundelstormwater
            return taxbill 
    else:
        taxbill = 0 
        return taxbill 
The exemptclass is coming back to Blank but it is showing it up as an array versus a straight value of "Blank". What am I missing

other files
limits.py
statelimit = 0.10
annearundelcountylimit = 0.02
rates.py
#state rates
statetaxrate = 0.1120

#anne arundel county rates
annearundeltaxrate = .93300
annearundelsolidwaste = 341
annearundelstormwater = 35.70
Reply
#2
(Mar-25-2023, 03:23 AM)mbrown009 Wrote: When I do it it is not hitting the conditions as expected.
Please be precise. What had you expected and what happens instead?
Reply
#3
(Mar-25-2023, 08:38 AM)ibreeden Wrote:
(Mar-25-2023, 03:23 AM)mbrown009 Wrote: When I do it it is not hitting the conditions as expected.
Please be precise. What had you expected and what happens instead?

When I get down to the functions it is getting into the first if. It should not. It should be getting into the second if where it is if exemptclass is equal to blank not if it is not equal to blank.

def owneroccupancycondition(x):
    if x == "H":
        return "Yes"
    elif x == "N":
        return " No "
    else:
        return "0"


def yearcondition(x):
    if x == "O":
        return "1899"
    else:
        return x


def homesteadqualiticationcondition(x):
    if x == "A":
        return "Approved"
    if x == "X":
        return "Denied"
    else:
        return "No Application"

def test(owneroccupied, homesteadcode, exemptclass, year3countyrealestate, year3staterealestate, year3countycredit, year3statecredit):
    from rates import statetaxrate, annearundeltaxrate, annearundelsolidwaste, annearundelstormwater

    if ("exemptclass" != "Blank"):
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill
    if ("exemptclass" == "Blank"):
        if ("owneroccupied" == "Yes"):
            taxbill = year3countyrealestate + year3staterealestate - year3countycredit - year3statecredit + annearundelsolidwaste + annearundelstormwater
            return taxbill
        else:
            taxbill = year3countyrealestate + year3staterealestate + annearundelsolidwaste + annearundelstormwater
            return taxbill 
    else:
        taxbill = 0 
        return taxbill 
Reply
#4
This is nonsensical. It will always be True because the string "exemptclass" will never equal the string "Blank".
if ("exemptclass" != "Blank"):
Did you mean to write this?
if (exemptclass != "Blank"):
Next time you post, do a better job. You post a bunch of code and the only description of the problem is :
Quote:I am pulling/wanting to pull in data from a pandas data frame to a straight variable so I can do pull it into a function so I do not duplicate code. When I do it it is not hitting the conditions as expected.
That might be meaningful to you. You know what conditions you are talking about. You know what you mean by "straight variable". You need to think about the person reading your post and make sure your description conveys all the required context. Something like this:

I have this function:
def test(owneroccupied, homesteadcode, exemptclass, year3countyrealestate, year3staterealestate, year3countycredit, year3statecredit):
    from rates import statetaxrate, annearundeltaxrate, annearundelsolidwaste, annearundelstormwater
 
    if ("exemptclass" != "Blank"):
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill
    if ("exemptclass" == "Blank"):
        if ("owneroccupied" == "Yes"):
            taxbill = year3countyrealestate + year3staterealestate - year3countycredit - year3statecredit + annearundelsolidwaste + annearundelstormwater
            return taxbill
        else:
            taxbill = year3countyrealestate + year3staterealestate + annearundelsolidwaste + annearundelstormwater
            return taxbill 
    else:
        taxbill = 0 
        return taxbill 
No matter what I pass for exemptclass, the function always returns this part.
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill
Reply
#5
(Mar-25-2023, 02:17 PM)deanhystad Wrote: Next time you post, do a better job. You post a bunch of code and the only description of the problem is :
Quote:I am pulling/wanting to pull in data from a pandas data frame to a straight variable so I can do pull it into a function so I do not duplicate code. When I do it it is not hitting the conditions as expected.
That might be meaningful to you. You know what conditions you are talking about. You know what you mean by "straight variable". You need to think about the person reading your post and make sure your description conveys all the required context. Something like this:

I have this function:
def test(owneroccupied, homesteadcode, exemptclass, year3countyrealestate, year3staterealestate, year3countycredit, year3statecredit):
    from rates import statetaxrate, annearundeltaxrate, annearundelsolidwaste, annearundelstormwater
 
    if ("exemptclass" != "Blank"):
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill
    if ("exemptclass" == "Blank"):
        if ("owneroccupied" == "Yes"):
            taxbill = year3countyrealestate + year3staterealestate - year3countycredit - year3statecredit + annearundelsolidwaste + annearundelstormwater
            return taxbill
        else:
            taxbill = year3countyrealestate + year3staterealestate + annearundelsolidwaste + annearundelstormwater
            return taxbill 
    else:
        taxbill = 0 
        return taxbill 
No matter what I pass for exemptclass, the function always returns this part.
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill

Thank you for the feedback. I will do a better job next time.
Reply
#6
I am have this function

def taxbill(owneroccupied, homesteadcode, exemptclass, year3countyrealestate, year3staterealestate, year3countycredit,
            year3statecredit):
    from rates import statetaxrate, annearundeltaxrate, annearundelsolidwaste, annearundelstormwater

    if (exemptclass != 'Blank'):
        taxbill = annearundelsolidwaste + annearundelstormwater
        return taxbill
    if (exemptclass == "Blank"):
        if (owneroccupied == "Yes"):
            taxbill = year3countyrealestate + year3staterealestate - year3countycredit - year3statecredit + annearundelsolidwaste + annearundelstormwater
            return taxbill
        else:
            taxbill = year3countyrealestate + year3staterealestate + annearundelsolidwaste + annearundelstormwater
            return taxbill
    else:
        taxbill = 0
        return taxbill
and when it runs I obtain the following error:

Error:
raise ValueError( ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
The data is being pulled from a pandas dataframe.

I am attempting to write this function to be able to avoid duplicate code when calculating the property tax for every year.

Any assistance would be wonderful
Reply
#7
Look at the type of exemptClass and think about what the error message is saying. Can a series be equal to "Blank"?
Reply
#8
(Mar-26-2023, 02:29 AM)deanhystad Wrote: Look at the type of exemptClass and think about what the error message is saying. Can a series be equal to "Blank"?

The original value of tanyardTH['exemptclass'] is equal to "Blank".

Blank is the text value in that column

Please see the attachment

Attached Files

Thumbnail(s)
   
Reply
#9
Using the data from your initial post I ran this program:
import pandas as pd

df = pd.read_csv("data.csv")
print(type(df['exemptclass']))
print(df['exemptclass'])
And it printed this:
Output:
<class 'pandas.core.series.Series'> 0 Blank Name: exemptclass, dtype: object
df['exemptclass'] is a Series object. It is like a list of strings. Even if the list only has one element, it cannot be compared to a string ('Blank'). You need to pass strings and floats to your function, not Series objects.

But instead of trying to figure out how to pass values to you function, I think it is time to take a step back and think about how you would use pandas to solve this problem. Instead of trying to pull information out of the dataframe, pass it to a function, and then maybe take the result and add it back into the dataframe, look for functions in pandas that can operate on your dataframe a row at a time. Maybe apply will work for you.

https://www.geeksforgeeks.org/apply-func...dataframe/
Reply
#10
(Mar-26-2023, 03:01 AM)deanhystad Wrote: Using the data from your initial post I ran this program:
import pandas as pd

df = pd.read_csv("data.csv")
print(type(df['exemptclass']))
print(df['exemptclass'])
And it printed this:
Output:
<class 'pandas.core.series.Series'> 0 Blank Name: exemptclass, dtype: object
df['exemptclass'] is a Series object. It is like a list of strings. Even if the list only has one element, it cannot be compared to a string ('Blank'). You need to pass strings and floats to your function, not Series objects.

But instead of trying to figure out how to pass values to you function, I think it is time to take a step back and think about how you would use pandas to solve this problem. Instead of trying to pull information out of the dataframe, pass it to a function, and then maybe take the result and add it back into the dataframe, look for functions in pandas that can operate on your dataframe a row at a time. Maybe apply will work for you.

https://www.geeksforgeeks.org/apply-func...dataframe/

similar to this?

cleandata['owneroccupancycode'] = cleandata['owneroccupancycode'].apply(owneroccupancycondition)
Reply


Forum Jump:

User Panel Messages

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