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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 29, 2013 at 23:07
1
  • 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. Commented Oct 23, 2013 at 4:15

3 Answers 3

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
answered Jul 29, 2013 at 23:24
9
  • 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:. Commented Jul 29, 2013 at 23:30
  • Not sure if it would be faster but it is more correct and will handle the case of None Commented Jul 29, 2013 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 Commented Jul 29, 2013 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. Commented Jul 29, 2013 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 :) Commented Jul 29, 2013 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
answered Jul 30, 2013 at 0:27
1
0

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

answered Jul 29, 2013 at 23:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.