1

I had a geodataframe having 4 columns (viz. name, time, description, geometry fiels). I am trying to export the geodataframe to ESRI Shapefile but it is throwing following error: DriverSupportError: ESRI Shapefile does not support datetime fields. The following is the code which I am using:

import geopandas as gpd

gdf = gpd.GeoDataFrame()
gdf['name'] = name_list
gdf['time'] = time_list
gdf['time'] = gdf['time'].dt.tz_localize(None)
gdf['description'] = description_list
gdf = gpd.GeoDataFrame(gdf,geometry=geometry_list)
gdf.crs = "epsg:4326"

# schema = gpd.io.file.infer_schema(gdf)
# print(schema)

gdf.to_file(r'C:\Users\Ashish\Desktop\Ground_Truth\test.shp', driver='ESRI Shapefile')

The "print(schema)" command shows the following: {'geometry': 'Point', 'properties': OrderedDict([('name', 'str'), ('time', 'datetime'), ('description', 'str')])}

GeoPandas version is '0.9.0', Fiona version is '1.8.19'

4
  • 4
    The message is true. ESRI shapefile format does not support datetime, only date. It is documented for example in desktop.arcgis.com/en/arcmap/latest/manage-data/shapefiles/….
    – user30184
    Feb 5, 2022 at 16:27
  • @user30184. Thanks for the reply. But as per this link "gis.stackexchange.com/questions/281895/…" it says it's possible with geopandas to have "datetime" as one of its field. But in my gdf the time field type is "datetime" but still it's not getting exported to shapefile.
    – RRSC NGP
    Feb 5, 2022 at 17:27
  • 1
    Shapefile, or more specifically, dBase-III+ cannot support higher than day resolution. It's not a GeoPandas limitation, but of the output format itself, as @user30184 has commented. Use a different, any different, output format.
    – Vince
    Feb 5, 2022 at 18:01
  • one way is to export as text and in the system that you are going to use, perform the conversion to datetime Oct 14, 2022 at 10:43

1 Answer 1

2

As @user30184 and @Vince mentioned above, the problem is the export format that does not support the datetime objects. The documentation from ESRI is a good help to understand the limits of the dBase III format (on which shapefiles are built).

This page also shows a distinction between shapefiles and geodatabases.

A shapefile stores dates in a date field with this format: yyyy-mm-dd.
A geodatabase formats the date as datetime yyyy-mm-dd hh:mm:ss AM or PM.

A workaround I've used is to simply convert the dataframe column with the date information to a string using strftime, formatted as requested by the shapefile. You did not provide a toy-code for us to try running it, but using your example, the solution would be something like this:

import datetime
import geopandas as gpd

gdf = gpd.GeoDataFrame()
gdf['name'] = name_list
gdf['time'] = time_list
gdf['time'] = gdf['time'].dt.tz_localize(None)
gdf['description'] = description_list
gdf = gpd.GeoDataFrame(gdf,geometry=geometry_list)
gdf.crs = "epsg:4326"
gdf['datetime'] = gdf['time'].dt.strftime("%Y-%m-%d")
gdf.to_file("some_shapefile.shp") #geopandas default format is shapefile, no need to set it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.