How to add a date field and custom date?

629
7
09-27-2022 08:46 AM
nsidaniel
New Contributor III

I'm trying to add a date field without it taking too long. Using arcpy.da.UpdateCursor took forever, so I'm trying Add field and calculate field ... which I cannot seem to get working! Any advice is appreciated! ArcGIS 2.9. Below is part of my code.

Thank you - Daniel

 

import arcpy, os, sys
import datetime

PatrolDateCalc = datetime.date(2022, 9, 19)
fc = oh
field = "PatrolDate"

if arcpy.ListFields(fc, field) == True:
    Print ("PatrolDate" + fc + "field already present")   
else:
    arcpy.management.AddField(fc, field, "DATE", "10", "", "", "Patrol Date", "", "NON_REQUIRED")
    arcpy.management.CalculateField(fc, field, PatrolDateCalc, "PYTHON3", "", "DATE", "NO_ENFORCE_DOMAINS")

fc = ug
field = "PatrolDate"
if arcpy.ListFields(fc, field) == True:
    Print ("PatrolDate" + fc + "field already present")   
else:
    arcpy.management.AddField(fc, field, "DATE", "10", "", "", "Patrol Date", "", "NON_REQUIRED")
    arcpy.management.CalculateField(fc, field, PatrolDateCalc, "PYTHON3", "", "DATE", "NO_ENFORCE_DOMAINS")


In  [14]:
Line:    arcpy.management.CalculateField(fc, field, PatrolDateCalc, "PYTHON3", "", "DATE", "NO_ENFORCE_DOMAINS")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField:
Line 5588:  raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField:
Line 5585:  retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type, enforce_domains), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a SQL Expression.
Failed to execute (CalculateField).

 

 

Tags (2)
0 Kudos
7 Replies
dgiersz_cuyahoga
Occasional Contributor

Where is "PatrolDateCalc" declared? I don't see it.

#CLE #sloth
nsidaniel
New Contributor III

Sorry about that! I forgot to include the definition here: I have updated the post to include PatrolDateCalc = datetime.date(2022, 9, 19).

0 Kudos
DonMorrison1
Occasional Contributor III

I assume it is complaining about 'PatrolDateCalc', which I done see being defined anywhere.

nsidaniel
New Contributor III

Thank you for responding! Yep, PatrolDateCalc is defined in my code, but I forgot to put it here. I've updated the post to include PatrolDateCalc = datetime.date(2022, 9, 19).

0 Kudos
dgiersz_cuyahoga
Occasional Contributor

Try using PatrolDateCalc = datetime.datetime(2022, 9, 19)

#CLE #sloth
0 Kudos
RogerDunnGIS
Occasional Contributor II

I don't often use the arcpy version of CalculateField, but I have three thoughts and ideas:

  1. Perhaps the "expression" parameter needs to be a string because I think another Python within arcpy has to eval() it.  Perhaps the expression parameter literally needs to be "datetime.date(2022, 9, 19)" instead of referencing a variable.
  2. If I am correct about the expression parameter, and you still want to use a variable as the date, perhaps you could pass in "datetime.date({0}, {1}, {2})".format(PatrolDateCalc.year, PatrolDateCalc.month, PatrolDateCalc.day) instead
  3. Is it a problem having a new field called DATE?  Sometimes this is a keyword in SQL databases

Let us know and good luck!

0 Kudos
RogerDunnGIS
Occasional Contributor II

Here is what worked for me on a file geodatabase on a field declared with the Date type:

arcpy.management.CalculateField(myLayer, myField, "'{0}'".format(PatrolDateCalc.strftime("%m/%d/%Y")))

 

Note that you may want (or even need) to change the parameter to strftime to match your locale better.  I don't know.

Let us know and good luck!

0 Kudos