0

I am creating a new field - let's call it A (SHORT INTEGER) in shp file using Modelbuilder and I need to fill it it with the values from two different fields. First step is to fill field A with the values from field B (that's a string field which is partially empty). The next step is to change all of the values (in A) which are 0, with values from another field with the result of equation C/3. Normally I would select the 0 values in the table and execute normal field calculation, but I'd like to have it as one statement in modelbuilder. That's what I wrote so far in Python

def function(A,C):
  if (A == 0):
    A == (C/3)

function(!A!,!C!)

I get the error that the field A is not nullable, but while creating it, the option is set to 'nullable'.

5

To update the field values, your function needs to include a return statement, like so.

def function(A,C):
  if (A == 0):
    A = (C/3)
    if A < 1:
      A = 1
  return A

The reason you get the 'The field is not nullable' error is that shapefiles don't support nulls and since your function didn't explicitly return anything, it is trying to Calculate values to None.

EDIT: Updated the expression to return 1 if C/3 is less than 1.

  • Thanks, a lot, now I don't get any more errors, but unfortunately the field is not being updated --> the 0 records remain 0. Any thoughts on that? – marziorro May 5 '15 at 18:21
  • @marziorro: Made an edit on the answer. Just need a logic switch to update A. – DWynne May 5 '15 at 18:35
  • 1
    I would suggest A = max(round(C/3.0), 1) -- more accurate division, and a floor of 1. – nmpeterson May 5 '15 at 20:01
  • That could depend on what @marziorro is looking for. If A is originally 0, and C is 5, do you expect a 1 or 2? I took it to mean 1, but it's not my workflow. (that said, using max is more elegant) – DWynne May 5 '15 at 22:54
2

There two things going on here. '==' evaluates truth, while '=' assigns the value.

A == (C/3)

Translates to: does A = C/3? No, this returns False.

As DWynne also points out, you need to return a value.

def function(A,C):
  if (A == 0):
    A = C/3
  return A

function(!A!,!C!)
  • yaay, worked! Thanks a lot! Men, don't know how could I oversee '==' and '='. Buuuut one more thing occurred. It returned 0 for the only one record form C, where it was 1 (1/3 is not possible for the integer). I have to assign value 1 then, shall i just add else return 1 ? – marziorro May 5 '15 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.