2

I am trying to clear entries in columns using the field calculator but with my current script, it removes all the data from all the rows. Basically, if the Historical column is equal to 'Historical Survey', I want it to replace the field text with '<Null>' but if the historical column is equal to 'Most recent survey', I want it to keep the text in that cell.

The full script is:

Expression line: reclass(!Key_Biomec!,!Historical!)

def reclass(Key_Biomec, Historical):
  if (Historical == 'Historical Survey'):
    return '<Null>'
  

Currently, this script removes all the data from the fields, no matter if they are labelled 'Historical Surveys' or 'Most recent surveys'. How to solve this issue?

1
  • Do you want the text "<Null>" or an actual NULL, represented by '<Null>'?
    – Vince
    Jul 15, 2021 at 11:59

2 Answers 2

4

You should always check to see that Calculate Field functions return a value in all possible condition paths. One way to do this is to use a code pattern that initializes the return value and uses if statements to alter it before a singleton return:

def reclass(key_biomec, historical):
    result = key_biomec
    if (historical == 'Historical Survey'):
        result = '<Null>' # or None, if an actual NULL value is needed
    return result

Note that Python best practice is to always use four spaces for indent and reserve uppercase names for object declaration.

4

Your code must be like below:

Expression line: reclass(!Historical!)

def reclass(Historical):
  if (Historical == 'Historical Survey'):
    return 'Null'
  else:
    return Historical
1
  • I just needed to change the expression line to include Key_Biomec and change the 'return' from Historical to Key_Biomec and it worked! Thank you for your help :) Jul 15, 2021 at 8:58

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.