3

How do I refer to Null values in a field using an expression in Python?

I've tried using the following as well as = None and =Null but nothing has worked yet:

ScoreCondition = "Cond_Score(!"+Condition+"!)"
codeblock1 = """def Cond_Score(cond):
                if cond > 90:
                    return 1
                elif cond >= 71 and cond <= 90:
                    return 2
                elif cond >= 51 and cond <= 70:
                    return 3
                elif cond >= 20 and cond <= 50:
                    return 4
                elif cond < 20:
                    return 5
                elif cond is None:
                    return 1"""
6
  • 1
    One equals sign is for assigning values. To test for equality, use two of them: ==
    – Dan C
    Commented Jan 10, 2019 at 21:47
  • I've confirmed all three of the following work when using the Field Calculator. Could it be an error somewhere else? Does the code work for the other condition? if cond == None: return 1 if cond is None: return 2 if not cond: return 3
    – MBuie
    Commented Jan 10, 2019 at 22:12
  • 1
    can't you just put an Else: for the last elif?
    – klewis
    Commented Jan 10, 2019 at 23:29
  • What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings. Commented Jan 10, 2019 at 23:57
  • 1
    Why is your code in strings?
    – jpmc26
    Commented Jan 11, 2019 at 2:49

1 Answer 1

10

Your problem is that in Python 2 None is less than everything.

>>> None < 20
True
>>> None < numpy.nan
True
>>> None < float('-inf')
True
>>> None < 'Everything'
True

Notes:

  • In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue.
  • This doesn't apply to shapefiles as the old dBase format .dbf that stores the attribute table doesn't support null values

In your expression, Python is returning True when evaluating the elif cond < 20 clause when cond is really None so never gets to the elif cond is None: clause.

So the following will work because it tests for None first:

def Cond_Score(cond):
    if cond is None:
        return 1
    elif cond > 90:
        return 1
    elif cond >= 71 and cond <= 90:
        return 2
    elif cond >= 51 and cond <= 70:
        return 3
    elif cond >= 20 and cond <= 50:
        return 4
    elif cond < 20:
        return 5

Your Answer

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

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