1

ArcPy under ArcGIS Pro 2.3.2.

I am adding a Python datetime.datetime object to a shapefile attribute table using an arcpy insert cursor. The problem is that when I add the datetime instance, the date is preserved in the attribute table, but the time is set to 0:00.

Here is how I create the field:

arcpy.AddField_management(outfile_points, field_name='timestamp',
 field_type='DATE',
 field_is_nullable='NON_NULLABLE')

Here is how I add the data:

 with arcpy.da.InsertCursor(outfile_points, field_list) as cursor:
 for a_storm in history.all_storms.values():
 for a_rec in a_storm.record_dict.values():
 data_list = [None] * len(field_list)
 data_list[field_dict[designation]] = a_storm.designation
 data_list[field_dict[timestamp]] = a_rec.record_time <===
 # Eliding irrelevant code
 cursor.insertRow(data_list)

The line with <==== is where I assign the value. Note, the type of .record_time is datetime.datetime.

When I check the value of data_list in this module as it runs, the time of day is preserved in the list. But when I view the data from ArcGIS Pro, the time of day is reset to 0:00.

Can anyone recommend what I should do differently at the <=== line?

The whole module is at CreateHurricanePolylines.py

nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Jun 5, 2019 at 22:31
1
  • 1
    The time is not preserved, it is just truncated, and a midnight value generated at read time. Commented Jun 5, 2019 at 23:56

1 Answer 1

4

This is a limitation of Shapefiles which cannot store both date and time in the same field.

The ESRI documentation at https://pro.arcgis.com/en/pro-app/help/data/tables/date-fields.htm says:

When calculating date fields, the field calculator uses Python datetime functions. Some of the functions support datetime yyyy-mm-dd hh:mm:ss AM or PM. However, for shapefiles, the time portion is truncated from the datetime value. For example, the datetime 2002年08月20日 12:00:00 PM is stored in a shapefile as 2002年08月20日.

So if you want to preserve the date and the time, you have a few options including:

  1. Use two separate fields, storing the time as a string in its own field

  2. Store the entire date and time as a single string in a text field instead of a date field

  3. Don't use Shapefiles

I would recommend using a file Geodatabase instead of a shapefile, if you have that option. Shapefiles are an old and very limited format. File geodatabases can be read and written by several other GIS packages these days.

answered Jun 5, 2019 at 22:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.