1

I'm trying to calculate multiple fields that all begin with "B01". This calculation consists of multiplying the fields by a field that contains a percentage ("TractPercent") and then dividing the results of that multiplication by a population field ("BufferPOP").

The problem I'm having is incorporating the variable associated with my "for loop" with my Python3 expression.

Here is my code:

import arcpy

### set workspaces
arcpy.env.workspace = r"C:\arcGIS_Shared\Python\Demographics.gdb"
relpath = r'C:\arcGIS_Shared\Python'
p = arcpy.mp.ArcGISProject(relpath + r'\Demographics.aprx')
m = p.listMaps('Map')[0]

layers = m.listLayers("DalyCity_HalfMile_Demographics")  
for layer in layers:
    print(layer)

    DemoFields = arcpy.ListFields(layer, "B01*", "ALL")
    for DemoField in DemoFields:
        arcpy.CalculateField_management(layer, DemoField, "(+DemoField+ * 
        !TractPercent!) / !BufferPOP!", "PYTHON3")
        print(layer.name + ' ' + DemoField + ' Calculated')

When I run the code this way I get the following error, but can't figure out what I need to change.

ERROR 000622: Failed to execute (Calculate Field). Parameters are not valid.
ERROR 000623: Invalid value type for parameter field
  • 1
    What's the format of the DemoField? What happens if you add some print statements - are you getting the values you expect? – smiller May 7 '18 at 15:20
  • @smiller I added some print statements and they returned what I expected them to. As far as the format, the data type is "Double". Is that what you were asking for? – Matt A. May 7 '18 at 15:41
  • @MattA., could you give us an example of what the print statement returned? And I think smiller meant to print the expression itself. Did you try testing the result directly in the Field Calculator, not using ArcPy? – Tom May 7 '18 at 16:20
5

Your expression shouldn't include variable names. You're writing:

"(+DemoField+ * !TractPercent!) / !BufferPOP!"

but it should be:

"(!{}! * !TractPercent!) / !BufferPOP!".format(DemoField.name)
  • 1
    Shouldn't it be DemoField.name? – Bjorn May 7 '18 at 15:44
  • I've tried both "(!{}! * !TractPercent!) / !BufferPOP!".format(DemoField) and "(!{}! * !TractPercent!) / !BufferPOP!".format(DemoField.name), but I am still getting the same errors. I looked at the data types associated with the CalculateField_management, (pro.arcgis.com/en/pro-app/tool-reference/data-management/…) but didn't see anything that stood out. Except for maybe that .format(DemoField) is outside the quotes – Matt A. May 7 '18 at 16:14
  • Thanks, Bjorn. I always get a list of names with list comprehension, so I overlooked that. It's fixed. @MattA., it's supposed to be outside the quotes. Otherwise, you're simply looking for a field that matches your variable name rather than the value held by the variable. – Tom May 7 '18 at 16:18
  • 1
    @MattA., what are the datatypes of each of your fields? – Tom May 7 '18 at 16:27
  • @Tom I tried running the code in the Code Block of the Field Calculator and got a similar error about a missing or incorrect parameter. The datatypes for all the fields I'm using in the code is "Double". As far as the print statements, I was able to successfully print the the Table name and the DemoField name. I'm not sure how I would print the expression, unless I assigned it to a variable. – Matt A. May 7 '18 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.