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

  • 1
    The time is not preserved, it is just truncated, and a midnight value generated at read time. – Vince Jun 5 at 23:56
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.

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.