I am running a field calculation which is [field A]/[field B]. Some of the values in field B are zero. Is there a script I could use to turn results in the calculation where the denominator is zero into -99 instead of creating an error message?

I am running a field calculation which is [field A]/[field B]. Some of the values in field B are zero. Is there a script I could use to turn results in the calculation where the denominator is zero into -99 instead of creating an error message?

- 2 people found this helpful
Something like:

Codeblock:

def my_function(numerator, denominator): if denominator == 0: denominator = -99 return numerator/denominator

Expression:

my_function( !YOUR_NUMERATOR_FIELD!, !YOUR_DENOMINATOR_FIELD! )

- 1 person found this helpful
Maybe the result can return just the value of

*-99*instead of*numerator/-99*if the denominator is 0. Rest seems perfect.- 2 people found this helpful
Oh, sure, yes I missed that part:

def my_function(numerator, denominator): if denominator == 0: return -99 else: return numerator/denominator

Maybe I am messing up the syntax. Here is what I have:

Codeblock:

def my_function(numerator, denominator):

if denominator == 0:

return -99

else:

return numerator/denominator

PRCHSCH =

my_function( [CHSCH04] , [TOSCH04] )

Still returning an error. Thanks for the help.

Should have mentioned you need to use the Python parser with this example, although to be fair you did tag Python . Field names should be enclosed in exclamation marks.

Darren -

I should have specified my level of experience with python. The formula makes sense but I am still messing up the syntax and I have tried it several ways. My fields are titled CHSCH04, which is the numerator, and TOSCH04, which is the denominator. Can you please tell me exactly how to put those in your script and in the formula? Thanks for your help.

Steve

- 2 people found this helpful
Put this in the code block:

def my_function(numerator, denominator): if denominator == 0: return -99 else: return numerator/denominator

...and this in the expression below the code block:

my_function(!CHSCH04!,!TOSCH04!)

Make sure you've changed to the Python parser.

That was my problem. I did not change to python.

Now there are no errors and those with no value in the denominator are coded as -99. The calculation returns a value of only 1 or 0 though. It is not rounding as only those values that should equal 1 are returning that. Every other value below 1 returns a 0. Additionally, the field is a float set to 6 decimal places.

Any ideas?

- 1 person found this helpful
Oh, that's probably a function of integer/integer=integer. If you change one to float it should return a float (float/integer=float and integer/float=float):

def my_function(numerator, denominator): if denominator == 0: return -99 else: return float(numerator)/denominator

- 3 people found this helpful
For simple cases like this, a Python Conditional Expression can be used directly in the field expression box, removing the need for a code block:

-99 if !TOSCH04! == 0 else float(!CHSCH04!)/!TOSCH04!

Thanks for the advice. Here is an additional step:

I would like to code an additional field (CHPROPSC) with multiple missing values. Essentially, if the value in PRCHSCH04 is -99 only then CHPROPSC = -98. If the value in PRCHSCH13 is -99 only then CHPROPSC = -97. If the value in both is -99 then CHPROPSC = -99. Otherwise CHPROPSC = PRCHSCH13 - PRCHSCH04.

I tried this but there was a processing failure:

-97 if !PRCHSCH13! == -99 & !PRCHSCH04! <> -99

-98 elif !PRCHSCH13! <> -99 & !PRCHSCH04! == -99

-99 elif !PRCHSCH13! == -99 & !PRCHSCH04! == -99

else float( !PRCHSCH13! )- !PRCHSCH04!

Any advice as to where the error is here?

Put this in the code block:

...and this in the expression below the code block:

Make sure you've changed to the Python parser.