Can I use a Numpy Expression in the Field calculator inside PRO?

1201
2
Jump to solution
04-29-2021 04:00 PM
mlopezr95984
New Contributor III

Hello,

I am trying to calculate a field in ArcGIS PRO. The field should contain a constant in every row. This constant will be equal to the maximum of another column in the table.dbf  WFr_100.

I can get to the number with this code, but I would like to do it through the field calculator inside Arcgis PRO.  Is there a way to do that?

# Import system modules

import arcpy

import numpy

# Set environment settings

arcpy.env.workspace = r"C:\Users\MD \Documents\ArcGIS\Projects\IP\Default.gdb"

input = "WFr_100" arr = arcpy.da.TableToNumPyArray(input, ('CMass'))

x=(arr["CMass"].max())

print(x)

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Do the work outside and bring it back in.

Close Pro to make sure any file locks are gone.

Verbose example

from arcpy.da import TableToNumPyArray, ExtendTable
tbl = r"C:\arcpro_npg\npg\Project_npg\npgeom.gdb\sample_10k"  # my table
arr = TableToNumPyArray(tbl, ["OID@", "Age"])  # the objectid field and another
arr1 = arr["OID@"]  # make oid values
arr2 = arr["Age"]   # Get the values you are interested
count = arr2.shape[0]  # get the length of the array
mx = np.nanmax(arr2)  # ---- now get the max... nanmax accounts for nulls
vals = np.repeat(mx, count) # ---- repeat the maximum "count" times
# create output  ---- excitement builds
z = np.empty_like(arr)  # build the output array
z.dtype.names = ("OID@", 'Max_Vals')  # make your output names
z["OID@"] = arr1    # put the id values in
z['Max_Vals'] = vals  # put the max in
ExtendTable(tbl, "OID@", z, "OID@")  # magic-orama ... like a join

Now open Pro and open the table to see the results.

max's.png

I have helper functions that expedite this, but you need to understand the procedure first.

numpy is extremely powerful and I have numerous blogs on its use with arcpy in the Python Blogs... look for me and me before retirement.


... sort of retired...

View solution in original post

Tags (2)
2 Replies
DanPatterson
MVP Esteemed Contributor

Do the work outside and bring it back in.

Close Pro to make sure any file locks are gone.

Verbose example

from arcpy.da import TableToNumPyArray, ExtendTable
tbl = r"C:\arcpro_npg\npg\Project_npg\npgeom.gdb\sample_10k"  # my table
arr = TableToNumPyArray(tbl, ["OID@", "Age"])  # the objectid field and another
arr1 = arr["OID@"]  # make oid values
arr2 = arr["Age"]   # Get the values you are interested
count = arr2.shape[0]  # get the length of the array
mx = np.nanmax(arr2)  # ---- now get the max... nanmax accounts for nulls
vals = np.repeat(mx, count) # ---- repeat the maximum "count" times
# create output  ---- excitement builds
z = np.empty_like(arr)  # build the output array
z.dtype.names = ("OID@", 'Max_Vals')  # make your output names
z["OID@"] = arr1    # put the id values in
z['Max_Vals'] = vals  # put the max in
ExtendTable(tbl, "OID@", z, "OID@")  # magic-orama ... like a join

Now open Pro and open the table to see the results.

max's.png

I have helper functions that expedite this, but you need to understand the procedure first.

numpy is extremely powerful and I have numerous blogs on its use with arcpy in the Python Blogs... look for me and me before retirement.


... sort of retired...
Tags (2)
mlopezr95984
New Contributor III

Spot on, Dan! Thank you.

0 Kudos