Select to view content in your preferred language

Python API field calculator

12-16-2019 04:09 PM
Occasional Contributor

I have been inclined to use the layer calculator since it is much faster than updating features using the API.  The feature update also fails if for example I try to update a large set of features at one time.  The layer calculator does the job much faster but I am having to scale the query as it limits the type of query.

here is what I have encountered a problem with.  I want to limit the calculation query by a date field and a time stamp or any way that makes comparing a field and a date possible.  How can I coerce the date data or the field to match a certain format? 

  Below is a code snippet

val =  # today's date

fld = 'todayis'  # featrue table field containng datetime

valx = datetime.strftime(,'%Y%m%d')

qry0 = "format({},'yyyyMMdd') <> '{}'".format(fld,val)

val2 = + timedelta(days=7)


result = lyr.calculate(where=qry0,calc_expression={"field" : fld2 , "value" : val2})

Running this code I get an error saying where parameter is invalid.  

'where' parameter is invalid
(Error Code: 400)

any insights?

0 Kudos
3 Replies
Occasional Contributor III


I think something is missing in your where query:

lyr.calculate(where="fld==qry0",calc_expression={"field" : fld2 , "value" : val2})‍‍
MVP Esteemed Contributor

A WHERE clause of "fld==qry0" is not correct for a couple of reasons.  First, the SQL equality operator is a single equal sign, =, and not a double, ==.  Second, the variable qry0 is a Python construct, and neither SQL nor the server know anything about it so it will generate an error.

0 Kudos
MVP Esteemed Contributor