0

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 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.

  • 1
    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. – smiller Jun 5 '18 at 13:18
  • 1
    Also NULL values don't exist in shapefiles, read this. – Hornbydd Jun 5 '18 at 13:22
  • @Hornbydd well I'm looking at the attribute table of this shapefile and the date field contains <Null> values. And it's not text. Maybe Nulls are possible for dates... – Theo F Jun 5 '18 at 13:54
  • @smiller sorry only looking to replace Null values. Some rows have dates already in them, I'd like to preserve those. – Theo F Jun 5 '18 at 13:57
  • @TheoF As commented above, Calculate Field respects the current selection. If you select for just the NULL values in Select by Attribute first, then go into the Field Calculator it will only calculate for the NULL fields. If you want to be on the safe side for this first test, copy the shapefile first. – smiller Jun 5 '18 at 14:05
1

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")
  • I'm not looking to replace all values, just ones that read Null (In my original post I wrote Null with two <'s surrounding the word, so actually that hid it from the post!) – Theo F Jun 5 '18 at 13:56
  • In the answer above I include the suggestion of using Select by Attributes to select the null values before you Calculate the field. Please advise what happens when you try that, or if there is a reason that it doesn't meet your needs. – smiller Jun 5 '18 at 14:01
  • I need this tool to run outside of an active ArcGIS Pro session. Select By Attributes noly works inside a session, as it refers to layers in the Table Of Contents, not feature classes stored on my drive. I should have clarified that. – Theo F Jun 5 '18 at 14:08
  • Yes, that does change the answer, but only slightly. I'll update my answer. 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 as noted by @Hornbydd, try the Select by Attribute within ArcGIS Pro first and copy/edit the Python code when finished. – smiller Jun 5 '18 at 14:12
  • Other methods: You could use the updateCursor instead of Field Calculator to check whether the field is NULL first. If you want more info on that let me know. Or you can use a code block within the Calculate Field step to check for Null. – smiller Jun 5 '18 at 14:26
1

I've found a python solution which works well: Replace all NULL values in a date field

I need to test this outside of the ArcGIS Pro session, but as it doesn't refer to layers, I'm assuming for now that it will work as a arcpy imported model.

  • This actually does refer to layers behind the scenes. But you can use a right click on the results to get the codeblock preformatted for CalculateField_management. We can discuss further in chat. – smiller Jun 5 '18 at 14:39
  • You can accept your own answer with the checkbox – BERA Jun 5 '18 at 18:14

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.