GeoEco.DataManagement.Fields.Field.CalculateField

classmethod Field.CalculateField(table, field, pythonExpression, where=None, modulesToImport=None, statementsToExecFirst=None, statementsToExecPerRow=None)

Calculates the value of a table field using a Python expression.

Parameters:
  • table (Table) – Table that contains the field to calculate.

  • field (str) – Field to calculate. Minimum length꞉ 1.

  • pythonExpression (str) –

    Python expression to evaluate for the specified field.

    For each row of the table, this function updates the values of the specified fields by evaluating their corresponding expressions using the Python eval function. In your expressions, you can access the value of any field by referencing the field as an attribute of the row object. For example, if your table contains a SampledSST field and you want to calculate the ActualSST field from it, you might calculate ActualSST with the following expression:

    row.SampledSST * 0.075 + 3.0
    

    Your expression may be any Python statement appropriate for passing to the eval() function. It must evaluate to a data type that is appropriate for the field’s data type:

    • For string, text, or character fields, return a str.

    • For integer fields, return an int.

    • For floating point fields, return a float.

    • For date or datetime fields, return datetime.

    • To set the field to a database NULL value, return Python None.

    For more information on Python syntax, please see the Python documentation. Minimum length꞉ 1.

  • where (str, optional) –

    SQL WHERE clause expression that specifies the rows to include. If not provided, all of the rows will be included. If provided but the underlying storage format does not support WHERE clauses, an exception will be raised.

    The exact syntax of this expression depends on the underlying storage format. If the underlying data store will be accessed through ArcGIS, this article may document some of the possible syntax, but not all of it may be supported through ArcGIS’s underlying Python API.

    Minimum length꞉ 1.

  • modulesToImport (list of str, optional) – Python modules to import prior to evaluating the expression. If you need to access Python functions or classes that are provided by a module rather than being built-in to the interpreter, list the module here. For example, to be able to use the datetime class in your expression, list the datetime module here. In your expression, you must refer to the class using its fully-qualified name, datetime.datetime.

  • statementsToExecFirst (list of str, optional) –

    Python statements to execute prior to looping through the rows of the table. The statements are executed sequentially using the Python exec() function. You can use Python statements to perform initialization tasks such as setting variables that you reference from your field expression. For example, you might want to perform a calculation on all of the rows that involves the current date and time, but you want the date and time to remain constant while the rows are being updated. To obtain the current date and time, you know you can import the datetime module and then invoke datetime.datetime.now(). But you do not want to put this into your field expression because the value will change as the system clock ticks during your computations. Instead you can set the now variable using the statement:

    now = datetime.datetime.now()
    

    and then reference it from your field expression. (Don’t forget to add datetime to the list of modules to import first.)

  • statementsToExecPerRow (list of str, optional) – Python statements to execute for every row after the row is retrieved but before the field expression is evaluated. The statements are executed sequentially using the Python exec() function. You can use Python statements to perform arbitrary tasks prior to evaluating your expression. For example, your table’s rows may represent files from which you want to extract a piece of metadata, but the extraction code cannot be expressed in a single statement. You could provide the Python statements needed to open each file, extract the metadata, and assign it to a variable. Your field expression could then reference the variable, causing the metadata value to be stored in the field.