Calculate Fields (multiple) (Data Management)

Summary

Calculates the values of two or more fields for a feature class, feature layer, or raster.

Usage

    Caution:

    This tool modifies the input data. See Tools that modify or update the input data for more information and strategies to avoid undesired data changes.

  • To learn more about Python expressions, see Calculate Field Python examples.

    To learn more about Arcade expressions, see ArcGIS Arcade in the Developer help.

    To learn more about SQL expressions, see Calculate field values.

    To learn more about VBScript expressions, see Calculate Field VBScript examples.

  • This tool's Arcade expressions use the Field Calculation Arcade profile. An Arcade profile is a context in which an Arcade expression is evaluated and understood. When an Arcade expression is evaluated, the return value is cast to the field type of the output value.

  • When used with a selected set of features, such as those created from a query using the Make Feature Layer or Select Layer By Attribute tool, this tool will only update the selected records.

  • Existing field values will be overwritten. To preserve the original values, make a copy of the input table as a backup, or use the Enable Undo option in the Geoprocessing pane.

  • For Python calculations, field names must be enclosed in exclamation points (for example, !fieldname!).

    For Arcade calculations, field names must be prefixed with $feature. (for example, $feature.fieldname).

  • Note:

    To transfer temporal values between field types (Date, Date Only, Time Only, Timestamp Offset, and Text), use the Convert Temporal Field tool instead.

  • To calculate strings to text or character fields, on the dialog box, the string must use double quotation marks (for example, "string"), or in scripting, the string using double quotation marks must also be enclosed in single quotation marks (for example, '"string"').

  • To calculate a field to be a numeric value, provide the numeric value in the Expression parameter with no quotation marks around the value.

  • You can create complex expressions using the Code Block parameter. Provide the code block either directly on the dialog box or as a string in scripting. The expression and code block are connected. The code block must relate back to the expression; the result of the code block must be passed to the expression.

    The Code Block parameter is only supported for Python expressions.

  • You can use the Python math module and formatting in the Code Block parameter. You can also 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.

  • To calculate geometry information in Python, use Geometry object properties. For example, use an expression of !shape.pointCount! to calculate the number of vertices in a feature.

    Note:

    The Calculate Geometry Attributes tool supports similar calculations.

    To calculate area and length in Python, use the getArea and getLength methods with a method and unit type.

    • To calculate the geodesic area of polygons in square kilometers, use the following expression:
      !shape.getArea('GEODESIC', 'SQUAREKILOMETERS')!
    • To calculate the planar length of polylines (or polygons) in yards, use the following expression:
      !shape.getLength('PLANAR', 'YARDS')

    See the ArcPy Polygon and Polyline objects for more information.

    Learn more about geoprocessing tools and linear and areal units

  • When working with joined data, you can only update fields from the origin table. You cannot update fields from the joined table. To perform a calculation on the joined table, perform the calculation directly on that table.

  • Python expressions that attempt to concatenate string fields that include a null or divide by zero value will return 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 sent to the server or database, resulting in faster calculations.

    Only feature services and enterprise geodatabases support SQL expressions. For other formats, use Python or Arcade expressions.

    Using the SQL option for the Expression Type parameter has the following limitations:

    • The option is only supported for Db2, Oracle, PostgreSQL, SAP HANA, and SQL Server enterprise geodatabases.
    • Calculating field values on joined tables is not supported.
    • Versioned and archived enterprise geodatabase data is not supported.
    • Undoing geoprocessing operations is not supported.

    See your database vendor documentation for SQL expression help.

Parameters

LabelExplanationData Type
Input Table

The table containing the fields that will be updated with the new calculations.

Table View; Raster Layer; Mosaic Layer
Expression Type

Specifies the type of expression that will be used.

If the input is a feature service, the default expression type is SQL. For any other type of input, the default expression type is Python.

To learn more about Python expressions, see Calculate Field Python examples.

To learn more about Arcade expressions, see ArcGIS Arcade in the Developer help.

To learn more about SQL expressions, see Calculate field values.

To learn more about VBScript expressions, see Calculate Field VBScript examples.

  • PythonThe Python expression type will be used.
  • ArcadeThe Arcade expression type will be used.
  • SQLThe SQL expression type will be used.
  • VBScriptThe VBScript expression type will be used.
String
Fields

The fields that will be calculated, the expressions to be calculated, and optional where clauses and field types.

The optional SQL expression will be used to select a subset of records. Only records that match this where clause will be calculated. If the where clause is left blank, all records will be calculated. For more information about SQL syntax, see SQL reference for query expressions used in ArcGIS.

If a field with the specified name does not exist in the input table, it will be added. The type of the field will be set using the field type value. If the field type is unspecified, the field will be added as a text field.

Available field types are as follows:

  • Short (16-bit integer)—The field type will be short. Short fields support whole numbers between -32,768 and 32,767.
  • Long (32-bit integer)—The field type will be long. Long fields support whole numbers between -2,147,483,648 and 2,147,483,647.
  • Big integer (64-bit integer)—The field type will be big integer. Big integer fields support whole numbers between -(253) and 253.
  • Float (32-bit floating point)—The field type will be float. Float fields support fractional numbers between -3.4E38 and 1.2E38.
  • Double (64-bit floating point)—The field type will be double. Double fields support fractional numbers between -2.2E308 and 1.8E308.
  • Text—The field type will be text. Text fields support a string of characters.
  • Date—The field type will be date. Date fields support date and time values.
  • Date (high precision)—The field type will be high precision date. High precision date fields support date and time values with millisecond time.
  • Date only—The field type will be date only. Date only fields support date values with no time values.
  • Time only—The field type will be time only. Time only fields support time values with no date values.
  • Timestamp offset—The field type will be timestamp offset. Timestamp offset fields support a date, time, and offset from a UTC value.
  • Blob (binary data)—The field type will be BLOB. BLOB fields support data stored as a long sequence of binary numbers. You need a custom loader or viewer or a third-party application to load items into a BLOB field or view the contents of a BLOB field.
  • GUID (globally unique identifier)—The field type will be GUID. GUID fields store registry-style strings consisting of 36 characters enclosed in curly brackets.
  • Raster imagery—The field type will be raster. Raster fields can store raster data in or alongside the geodatabase. All ArcGIS software-supported raster dataset formats can be stored, but it is recommended that only small images be used.
Value Table
Code Block
(Optional)

A block of code that will be used for complex expressions.

A function cannot be used to return multiple values.

String
Enforce Domains
(Optional)

Specifies whether field domain rules will be enforced.

  • Checked—Field domain rules will be enforced. If a field cannot be updated, the field value will remain unchanged, and the tool messages will include a warning message.
  • Unchecked—Field domain rules will not be enforced. This is the default
Boolean

Derived Output

LabelExplanationData Type
Output Table

The updated table.

Table View; Raster Layer; Mosaic Layer

arcpy.management.CalculateFields(in_table, expression_type, fields, {code_block}, {enforce_domains})
NameExplanationData Type
in_table

The table containing the fields that will be updated with the new calculations.

Table View; Raster Layer; Mosaic Layer
expression_type

Specifies the type of expression that will be used.

  • PYTHON3The Python expression type will be used.
  • ARCADEThe Arcade expression type will be used.
  • SQLThe SQL expression type will be used.
  • VBThe VBScript expression type will be used.

If the input is a feature service, the default expression type is SQL. For any other type of input, the default expression type is PYTHON3.

To learn more about Python expressions, see Calculate Field Python examples.

To learn more about Arcade expressions, see ArcGIS Arcade in the Developer help.

To learn more about SQL expressions, see Calculate field values.

To learn more about VBScript expressions, see Calculate Field VBScript examples.

String
fields
[[Field Name, Expression, {Where Clause}, {Field Type}],...]

The fields that will be calculated, the expressions to be calculated, and optional where clauses and field types.

The optional SQL expression will be used to select a subset of records. Only records that match this where clause will be calculated. If the where clause is left blank, all records will be calculated. For more information about SQL syntax, see SQL reference for query expressions used in ArcGIS.

If a field with the specified name does not exist in the input table, it will be added. The type of the field will be set using the field type value. If the field type is unspecified, the field will be added as a text field.

Available field types are as follows:

  • SHORT—The field type will be short. Short fields support whole numbers between -32,768 and 32,767.
  • LONG—The field type will be long. Long fields support whole numbers between -2,147,483,648 and 2,147,483,647.
  • BIGINTEGER—The field type will be big integer. Big integer fields support whole numbers between -(253) and 253.
  • FLOAT—The field type will be float. Float fields support fractional numbers between -3.4E38 and 1.2E38.
  • DOUBLE—The field type will be double. Double fields support fractional numbers between -2.2E308 and 1.8E308.
  • TEXT—The field type will be text. Text fields support a string of characters.
  • DATE—The field type will be date. Date fields support date and time values.
  • DATEHIGHPRECISION—The field type will be high precision date. High precision date fields support date and time values with millisecond time.
  • DATEONLY—The field type will be date only. Date only fields support date values with no time values.
  • TIMEONLY—The field type will be time only. Time only fields support time values with no date values.
  • TIMESTAMPOFFSET—The field type will be timestamp offset. Timestamp offset fields support a date, time, and offset from a UTC value.
  • BLOB—The field type will be BLOB. BLOB fields support data stored as a long sequence of binary numbers. You need a custom loader or viewer or a third-party application to load items into a BLOB field or view the contents of a BLOB field.
  • GUID—The field type will be GUID. GUID fields store registry-style strings consisting of 36 characters enclosed in curly brackets.
  • RASTER—The field type will be raster. Raster fields can store raster data in or alongside the geodatabase. All ArcGIS software-supported raster dataset formats can be stored, but it is recommended that only small images be used.
Value Table
code_block
(Optional)

A block of code that will be used for complex expressions.

A function cannot be used to return multiple values.

String
enforce_domains
(Optional)

Specifies whether field domain rules will be enforced.

  • ENFORCE_DOMAINSField domain rules will be enforced.
  • NO_ENFORCE_DOMAINSField domain rules will not be enforced. This is the default.
Boolean

Derived Output

NameExplanationData Type
out_table

The updated table.

Table View; Raster Layer; Mosaic Layer

Code sample

CalculateFields example 1 (Python window)

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.management.CalculateFields("parcels", "PYTHON3", 
                                 [["xCentroid", "!SHAPE.CENTROID.X!"], 
                                  ["yCentroid", "!SHAPE.CENTROID.Y!"]])
CalculateFields example 2 (Python window)

The following Python window script demonstrates how to use the CalculateFields function in immediate mode with the SQL expression type.

import arcpy
arcpy.management.CalculateFields("<a feature service url>", "SQL", 
                                 [["ceiling_field", "CEILING(field1)"], 
                                  ["floor_field", "FLOOR(field1)"]])
CalculateFields example 3 (Python window)

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.management.CalculateFields(
    "parcels", "ARCADE", 
    [["max_value", "Max($feature.field1, $feature.field2)"], 
     ["min_value", "Min($feature.field1, $feature.field2)"]])
CalculateFields example 4 (Python window)

The following Python window script demonstrates how to use the CalculateFields function with SQL expressions.

import arcpy
arcpy.env.workspace = "C:/data/airport.gdb"

# Calculate the Field1 field to -1 if the value is null.
# Calculate the Title field to remove any leading spaces if the first character
# is a space.
arcpy.management.CalculateFields(
    "parcels", "PYTHON3", 
    [['Field1', -1, "Field1 IS NULL"],
     ['Title', '!Address!.lstrip()', "Title LIKE ' %'"]])

Related topics