1

I'm trying to write a conditional statement that references another field within the same shapefile layer. I want it to only do so if the one field is " Null " and leave is alone if there is already a number there. I would think there is a simple way to do this, but I'm having trouble.

Did anyone have a similar problem they figured out?

  • I have a suspicion that using the Python parser in the Field Calculator to work with Null values had a 10.0 bug which was fixed at 10.1. If you have any issues in this vicinity at 10.0 then I recommend testing the same code at 10.1/10.2. I came across this here and in my own work recently. – PolyGeo Oct 23 '13 at 4:15
4

You should look at using the codeblock in the field calculator. I think it's easiest using the python parser.

Have a look at the examples on this page.

You could do something like this:

Parser: Python

Expression:

calc(!FIELD!)

Code Block:

def calc(myField):
  if (myField == ""):
    return someValue
  else:
    return myField
  • 1
    You need an else statement in there, otherwise you'll get an error. Also, it should be slightly faster to use if not myField:. – Paul Jul 29 '13 at 23:30
  • Not sure if it would be faster but it is more correct and will handle the case of None – Nathan W Jul 29 '13 at 23:38
  • @NathanW, I ran the following through timeit: test = ['' if i%2 else i for i in xrange(10000)] as setup for [m for m in test if not m] and [m for m in test if m=='']. 586µs vs 1.06ms – Paul Jul 29 '13 at 23:47
  • 1
    @Paul, an if statement is valid without an else statement. If myfield doesn't equal "" then it just doesn't return anything. – Fezter Jul 29 '13 at 23:53
  • @Paul seems it's a little faster i.imgur.com/r0TRFFH.png but both are so fast you shouldn't care and only go with what is best style, which is if not myField like you said :) – Nathan W Jul 29 '13 at 23:57
2

+1 @Fetzer.

I'm not sure if this is purely a shapefile "phenomenon" (I rarely if ever mess with GDB--they have nullable fields at any rate), but cells that appear to be blank are not really blank.

This can be shown by adding a blank field and then running a field calculator on it, setting the values to "". If you look at the values afterwards, they are actually being stored as single spaces, which can be seen via a cursor or during an edit session.

You can go and delete the space, save your edits, and it will still come back.

If the above code seems to be failing for this reason, you'll need to make a slight adjustment:

Expression:

calc(!FIELD!)

Code Block:

def calc(myField):
  if not myField:
    return someValue
  elif myField == " ":
    return someValue
  else:
    return MyField

If you know that the cells are stored as a true "blank cell" then you can use ternary operators:

def calc(myField):
  return someValue if not myField else MyField

or if some cells are stored as a single space:

def calc(myField):
  return someValue if myField == " " else MyField
0

Use select by attributes. Choose the field with the blanks, set = "" Then use field calculate on selected rows.

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.