Reclass null values in text field using Calculate Field

3847
5
12-05-2017 04:26 AM
DataOfficer
Occasional Contributor III

I have a text field called 'Description' that contains some <Null> values that I wish to update to 'Hedgerows' while keeping all other values the same.

Using Calculate Field in Pro, I tried the following:

Expression: 

Reclass (!Description!)

Code block

def Reclass(Description):

if (IsNull)

return Hedgerows

The expression was successfully verified but I then got the following error message when running the tool:

Error 00539: Error running expression: Reclass (u"Buildings")

Traceback (most recent call last):

File "<expression>", line 1, in <module>

File "<string>, line 2, in Reclass

Name Error: name 'IsNull' is not defined

Failed to execute (CalculateField).

Any suggestions? I'm a complete novice when it comes to Python. 

0 Kudos
5 Replies
TedKowal
Occasional Contributor III

Much Ado About Nothing   is a excellent document written by curtvprice  Dan Patterson‌  describing techniques in dealing with Null values.

def reclass (myString):
    if myString is None:
         return 'Hedgerow'
    return myString


reclass(!Description!)‍‍‍‍‍‍‍‍‍‍‍‍‍‍
DataOfficer
Occasional Contributor III

Thank you, I will try this now! Just out of interest, how would this code change if dealing with numerical values rather than string? 

0 Kudos
TedKowal
Occasional Contributor III

Lot of ways to appoach it ... one way is to make the number a string... but the test doesnt care whether or not the variable is a number or string  so testing myNumber is None would be valid as well -- It is what you return to the field that is important.

def reclass (myNumber):
    if str(myNumber) is None:
         return 911
    return myNumber


reclass(!NumberField!)
0 Kudos
KoryKramer
Esri Community Moderator

Since you're a novice to python, I just wanted to make sure you were aware of the Helpers in Pro:

Adding your story about Reclass on this idea ArcGIS Pro Field Calculate Helpers‌ would be worthwhile.

The reclass example is in the doc: Calculate Field examples—Data Management toolbox | ArcGIS Desktop  (Ctrl+F 'reclass')

That doc on examples might be helpful to you in the future as well. 

Cheers

DataOfficer
Occasional Contributor III

Thanks - this is the example I tried to follow but wasn't sure how to handle 'Null' values (in a string or numeric field). I think I'm beginning to get a better sense of it. What I would really like to see is a feature class version of the 'Reclassify' tool so users that aren't familiar with Python can easily define 'Start' and 'End' values without using code. 

Anyway, thanks for the help!

0 Kudos