Python – How to Replace NULL Values in a DATE Field in ArcGIS Pro

arcgis-profield-calculatorpython

In a date field (TRTMT_DT) in a shapefile, how can I use ArcGIS Pro to replace all Null values with a filler date of '11/11/1111'?

For my workflow I can't have any entries in this date field being Null.
I see there is no .replace function for non-string fields..
For example, in the Calculate Field tool, the following code doesn't work:

!TRTMT_DT!.replace('', '11/11/1111')

I need this to work outside of an active ArcGIS Pro session, referring to feature classes stored on disk, not referring to layers in the Table Of Contents pane. My intention is to call the process as a model.

Best Answer

If you are replacing all values, just enter the date in Calculate Field tool -- no need to reference the field in code or a codeblock. If you need to do this for a subset of values, you can use Select by Attributes before the Calculate Field. Calculate field respects the current selection.

TRTMT_DT = 11/11/1111

Note: the date will appear as 12/30/1899, at least in my testing on both GDB and shapefile. Possible reasons explained at this link.

UPDATE: Original post didn't include that this is to be run in arcpy, outside of an interactive ArcGIS Pro session. Update below accounts for that.

Use MakeFeatureLayer_management to create a layer, then Select Layer by Attribute for the Null values, then Calculate Field. In case the NULL values are formatted weird for date fields in shapefiles as noted by @Hornbydd, try the Select by Attribute within ArcGIS Pro first and copy/edit the Python code when finished.

Example code:

arcpy.MakeFeatureLayer_management(fc, "lyr")
arcpy.SelectLayerByAttribute_management("lyr",
                                                "NEW_SELECTION", "TRTMT_DT IS NULL")
arcpy.CalculateField_management("lyr", "TRTMT_DT", 
                                                '11/11/1111', "PYTHON3")
Related Question