Summary
Calculates the values of fields for a feature class, feature layer, or raster.
Usage
To learn more about Python expressions, see Calculate Field Python examples.
To learn more about Arcade expressions, see the ArcGIS Arcade guide.
To learn more about SQL expressions, see Calculate field values.
When used with a selected set of features, such as those created from a query in the Make Feature Layer or Select Layer By Attribute tool, this tool will only update the selected records.
Existing field values will be overwritten. Make a copy of the input table if you want to preserve the original values.
For Python calculations, field names must be enclosed in exclamation points (!fieldname!).
For Arcade calculations, field names must be prefixed with $feature. (for example, $feature.fieldname).
To calculate strings to text or character fields, on the dialog box, the string must use double quotation marks ("string"), or in scripting, the string using double quotation marks must also be enclosed in single quotation marks ('"string"').
To calculate a field to be a numeric value, enter the numeric value in the Expression parameter; no quotation marks around the value are required.
The Code Block parameter allows you to create complex expressions. You can enter the code block directly on the dialog box or as a continuous string in scripting. The expression and code block are connected. The code block must relate back to the expression; pass the result of the code block into the expression.
The Code Block parameter is only supported for Python expressions.
The Python math module and formatting are available for use in the Code Block parameter. You can import additional modules. The math module provides number-theoretic and representation functions, power and logarithmic functions, trigonometric functions, angular conversion functions, hyperbolic functions, and mathematical constants. To learn more about the math module, see the Python help.
Python expressions can be created using properties from the Geometry object, including type, extent, centroid, firstPoint, lastPoint, area, length, isMultipart, and partCount (for example, !shape.area!).
Python expressions can use the geometry area and length properties with an areal or linear unit to convert the value to a different unit of measure (for example, !shape.length@kilometers!). If the data is stored in a geographic coordinate system and a linear unit is supplied (for example, miles), the length will be calculated using a geodesic algorithm. Using areal units on geographic data will yield questionable results, as decimal degrees are not consistent across the globe.
- Areal unit of measure keywords:
- ACRES | ARES | HECTARES | SQUARECENTIMETERS | SQUAREDECIMETERS | SQUAREINCHES | SQUAREFEET | SQUAREKILOMETERS | SQUAREMETERS | SQUAREMILES | SQUAREMILLIMETERS | SQUAREYARDS | SQUAREMAPUNITS | UNKNOWN
- Linear unit of measure keywords:
- CENTIMETERS | DECIMALDEGREES | DECIMETERS | FEET | INCHES | KILOMETERS | METERS | MILES | MILLIMETERS | NAUTICALMILES | POINTS | UNKNOWN | YARDS
- Areal unit of measure keywords:
Python expressions can be used to calculate the geodesic area or length of a feature using the geodesicArea or geodesicLength properties combined with areal or linear units of measure (for example, !shape.geodesicArea@hectares! or !shape.geodesicLength@miles!).
When calculating joined data, you cannot directly calculate the joined columns. However, you can directly calculate the columns of the origin table. To calculate the joined data, you must first add the joined tables or layers to the map. You can then perform calculations on this data separately. These changes will be reflected in the joined columns.
Python expressions that attempt to concatenate string fields that include a null, or divide by zero, will return a null for that field value.
SQL expressions support faster calculations for feature services and enterprise geodatabases. Instead of performing calculations one feature or row at a time, a single request is set to the server or database, resulting in significantly faster calculations.
Only feature services and enterprise geodatabases support SQL expressions. For other formats, use Python or Arcade expressions.
Using the Expression Type parameter's SQL option has the following limitations with enterprise geodatabases:
- The option is only supported for Db2, Oracle, PostgreSQL, SAP HANA, and SQL Server.
- Calculating field values on joined tables is not supported.
- Versioned data is not supported.
- The ability to undo geoprocessing operations is not supported.
See your database vendor documentation for SQL expression help.
Syntax
arcpy.management.CalculateFields(in_table, expression_type, fields, {code_block})
Parameter | Explanation | Data Type |
in_table | The table containing the fields that will be updated with the new calculation. | Table View; Raster Layer; Mosaic Layer |
expression_type | Specify the type of expression that will be used.
To learn more about Python expressions, see Calculate Field examples. To learn more about Arcade expressions, see the ArcGIS Arcade guide. To learn more about SQL expressions, see Calculate field values. | String |
fields [[Field Name, Expression],...] | The fields to be calculated and their expressions. | Value Table |
code_block (Optional) | A block of code for complex expressions. A function cannot be used to return multiple values. | String |
Derived Output
Name | Explanation | Data Type |
out_table | The update tables. | Table View; Raster Layer; Mosaic Layer |
Code sample
The following Python window script demonstrates how to use the CalculateFields function in immediate mode with the Python expression type.
import arcpy
arcpy.env.workspace = "C:/data/airport.gdb"
arcpy.CalculateFields_management("parcels", "PYTHON3",
[["xCentroid", "!SHAPE.CENTROID.X!"],
["yCentroid", "!SHAPE.CENTROID.Y!"]])
The following Python window script demonstrates how to use the CalculateFields function in immediate mode with the SQL expression type.
import arcpy
arcpy.CalculateFields_management("<a feature service url>", "SQL",
[["ceiling_field", "CEILING(field1)"],
["floor_field", "FLOOR(field1)"]])
The following Python window script demonstrates how to use the CalculateFields function in immediate mode with the Arcade expression type.
import arcpy
arcpy.env.workspace = "C:/data/airport.gdb"
arcpy.CalculateFields_management(
"parcels", "ARCADE",
[["max_value", "Max($feature.field1, $feature.field2)"],
["min_value", "Min($feature.field1, $feature.field2)"]])
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes