ArcGIS Pro Field Calculator just converts entire column to null values

247
5
Jump to solution
3 weeks ago
NeoGeo
by
Occasional Contributor III

This is a really short script but I cannot seem to make it work in field calculator no matter what.  No errors are generated but it converts all rows in the column to null values even if they are numbers (like a zip code plus 4 without the hyphen that just needs a hyphen added which is in a 10-digit Text field).  

def ZipCodeFix(MyInput):
    if MyInput is None:
        pass
    elif MyInput == '':
        return None
    else:
        NoSpaces=MyInput.strip()
        if len(NoSpaces) == 9:
            output = NoSpaces[:5] + '-' + NoSpaces[5:]
            return output
    else:
        pass

0 Kudos
1 Solution

Accepted Solutions
NeoGeo
by
Occasional Contributor III

I had tried converting everything to strings even though they were coming from a text field, but it had not helped. 

The problem seems to be in the pass.  I don't know if I am just remembering incorrectly, but I do not recall it ever working this way before (in Desktop with Python 2.x).  I may be wrong, but I was thinking that in the past, if I did a pass it would skip that row and leave the existing value in the cell, but it apparently does not work that way here in Pro/Python 3.  I have to explicitly return the previous value of the cell in cases that I want to leave it unchanged or it nulls the cell.  

View solution in original post

0 Kudos
5 Replies
CodyPatterson
Occasional Contributor III

Hey @NeoGeo 

I threw this into a test table, and I believe it may be an issue with the else at the bottom not being within the if statement if the formatting on the question is how it really is, here is what I have now:

def ZipCodeFix(MyInput):
    if MyInput is None:
        pass
    elif MyInput == '':
        return None
    else:
        NoSpaces = MyInput.strip()
        if len(NoSpaces) == 9:
            output = NoSpaces[:5] + '-' + NoSpaces[5:]
            return output
        else:
            pass

I moved the else statement at the bottom into the function, and my process began working in my environment. If yours acts different let me know and I'll check it out!

Hope this helps!

Cody

0 Kudos
NeoGeo
by
Occasional Contributor III

It appears to be structured the same in field calculator as yours, it just got messed up here since there is no code block for this forum as far as I can tell.  I have run it without the else and the pass also.  

I just decided to run it in an IDE passing it a 9 digit number, while still using ArcGIS's Python version, but not running it inside ArcGIS Pro and I still don't get output.  Very strange.  I wonder if it is a bug in this Python 3 version.

0 Kudos
CodyPatterson
Occasional Contributor III

Hey @NeoGeo 

Could it possibly be that the inputs are integers? If so, you may need to convert it to a string, this function would only be able to make operations on a string since it uses .strip().

I put this inside of a Python IDE and tested with num1 = "123456789" and num2 = 123456789, num1 worked and num2 didn't, as a catch, you can include MyInput = str(MyInput), like this here:

def ZipCodeFix(MyInput):
    MyInput = str(MyInput)
    if MyInput is None:
        pass
    elif MyInput == '':
        return None
    else:
        NoSpaces = MyInput.strip()
        if len(NoSpaces) == 9:
            output = NoSpaces[:5] + '-' + NoSpaces[5:]
            return output
        else:
            pass

Hope that helps!

Cody

0 Kudos
NeoGeo
by
Occasional Contributor III

I had tried converting everything to strings even though they were coming from a text field, but it had not helped. 

The problem seems to be in the pass.  I don't know if I am just remembering incorrectly, but I do not recall it ever working this way before (in Desktop with Python 2.x).  I may be wrong, but I was thinking that in the past, if I did a pass it would skip that row and leave the existing value in the cell, but it apparently does not work that way here in Pro/Python 3.  I have to explicitly return the previous value of the cell in cases that I want to leave it unchanged or it nulls the cell.  

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Even with ArcGIS Desktop/ArcMap, if you define a function with only a pass statement and nothing else, it will return None and then Calculate Field turns that None into a SQL NULL.  Although Calculate Field does iterate over the records, your pass statement is in a function that is being called on each row and not within the loop itself.  A Python function has to return something, either a value or an error.  If you don't return something, then Python None is returned.