calculate function in python jupyter notebook

4053
10
Jump to solution
04-09-2020 03:58 PM
DryCreekEng
Occasional Contributor

Hi everyone,

I am trying to create a script to run every night to calculate the total insects counted each day by summing two insect  count fields in a feature layer.  I can't use Arcade because the layer has "track changes" and "sync" enabled for the field crew (grrrr...). Below is my simple script to calculate the "TotalArmyworms" field based on summing the Western Yellow Armyworm field with the Bertha Armyworm field. I have tried many different ways to identify the layer from AGOL, and each time the error states that 'Item' object has no attribute 'calculate'.  Any thoughts?

import arcgis
import arcpy
from arcgis.gis import GIS
from arcgis import features
from arcgis.features import FeatureLayer

gis = GIS('https://arcgis.com', 'xxxxxx', 'xxxxxx')
arcpy.env.overwriteOutput = True

trap_count = gis.content.get("b6b9c68fa42b408a88c68aa9e18593d5")
trap_count

trap_count.calculate(where="fid > 0", calc_expression=[{"field": "TotalArmyworms", "sqlExpression" : "Nu_W_YellowStripedArmyworm + Nu_BerthArmyworm" }])
   

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in __getattr__(self, name)
   6998         try:
-> 6999             return dict.__getitem__(self, name)
   7000         except:

KeyError: 'calculate'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-50-2608a815f7ee> in <module>
----> 1 trap_count.calculate(where="fid > 0", calc_expression=[{"field": "TotalArmyworms", "sqlExpression" : "Nu_W_YellowStripedArmyworm + Nu_BerthArmyworm" }])
      2 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in __getattr__(self, name)
   6999             return dict.__getitem__(self, name)
   7000         except:
-> 7001             raise AttributeError("'%s' object has no attribute '%s'" % (type(self).__name__, name))
   7002 
   7003     def __getitem__(self, k): # support item attributes as dictionary keys on this object

AttributeError: 'Item' object has no attribute 'calculate'

1
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (1)
0 Kudos
10 Replies
erica_poisson
Occasional Contributor III

Hi Natalie & William,

I was able to accomplish my goal using the following:

import arcpy
import pandas as pd
from arcgis import features
from arcgis.features import FeatureLayerCollection
from arcgis.features import FeatureLayer
from arcgis.gis import GIS

# #### Run to process data:

gis = GIS('https://arcgis.com/', 'xxxxx', 'xxxxx')

# connect to feature layer and select proper table
item=gis.content.get('a550ded747db465eb2ff62153b559095')
l=item.tables[0]

# need to deal with null Unknown_Number because Total_Gull_Count does not properly calculate if left null.
unknown=l.query(where="Unknown_Number IS NULL").sdf

# calculate null Unknown_Numbers to be equal to 0.
print(l.calculate(where="Unknown_Number IS NULL",
                 calc_expression={"field": "Unknown_Number", "sqlExpression" : '0'}))

# calculate field Total Gull Count
# select records where Total_Gull_Count is null, and then calculate value
print(l.calculate(where="Total_Gull_Count IS NULL",
                 calc_expression={"field": "Total_Gull_Count", "sqlExpression" : "RB_numb + HG_numb + BB_numb + Unknown_Number" }))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Erica