English

Problem: Field Calculator completes but values do not change

Description

The Field Calculator is a great tool that can be used to modify field values all at once without taking the time for individual calculations. However, using the Field Calculator requires careful awareness of details such as field type, field length, and domains. In some situations, a user may see that the Field Calculator ran with no errors, but the field values have not changed.

Cause

The caveat to using Field Calculator is that the user must be aware of the types of fields being consumed.

If, for example, a user has a table with two columns and wants to fill out a third with text only if the first two match:
Image showing the input table with three fields.

Using comparison code in Field Calculator:

Image showing code in Field Calculator where, within the code block, the statement "if Field1==Field2:".
This completes and appears to have succeeded in the Geoprocessing menu > Results, but the third column does not change.
Results window showing that Field Calculator completed successfully.
This is because the two input fields, though in some cases may appear the same (1 in FieldName1 and 1 in FieldName 2), they are different field types:
Screenshot of FieldName1 properties showing Type: StringScreenshot of FieldName2 properties showing Type: Short
Therefore, the string "1" is not equal to the short integer "1", and because there is no "else" statement to go with the "if" statement in the code block, nothing is returned to populate the field.

Solution or Workaround

Always check the field types for the inputs into the Field Calculator. Field types can be temporarily modified using code such as str(), int(), etc., to convert inputs to the same type for successful field calculation. In this example, an "else" statement can be added.
Image showing the added line "else: return "The values are different""
This then provides an output when the "if" statement is false, which could indicate that that further research is needed, specifically investigating the reason why the if statement is false, thus investigating the values and their types.
Image showing the third column now is populated with "The values are different"

Related Information