Created a GeoPandas dataframe from an excel sheet. The schema for the dataframe is:
{'geometry': 'Point',
'properties': OrderedDict([
...
('DISTID', 'int'),
('PROVID', 'int'),
('date', 'datetime')])}
Edit: Thought I should clarify - the date field is saved as type datetime, but it only has date.
But when I run gdf.to_file('sample.shp', driver = "ESRI Shapefile")
I get the error DriverSupportError: ESRI Shapefile does not support datetime fields
.
I tried converting the datetime field to date by using gdf['date'] = pd.to_datetime(gdf['date']).dt.date
but then that throws a ValueError: Invalid field type <class 'datetime.date'>
. Kind of stumped on what to do, I'd like to keep the dates in the Shapefile. I'm using GeoPandas version 0.11.0 and Fionas 1.8.21.
Edit 2: Should have been more specific with what I tried. I attempted to change the field type using the solutions here Changing shapefile's field type using fiona? to no avail. If I try to change the schema, I get ValueError: Record does not match collection schema:
.
I then tried to use the gdf['date'] = pd.to_datetime(gdf['date']).date()
but the fucntion didn't even run, giving me AttributeError: 'Series' object has no attribute 'date'
. Then I did gdf['date'] = pd.to_datetime(gdf['date']).dt.date
, and while the function worked, I got ValueError: Invalid field type <class 'datetime.date'>
when trying to export as a shapefile.
Edit 3:
the entire excel file, when exported as a pandas dateframe, has only one field 'date' with type datetime64[ns]
. The dates are all in the form . They do not have any time attached.
I do the following:
pdf = pd.read_excel("data.xlsx", index_col=0)
gdf = gpd.GeoDataFrame(pdf, geometry=gpd.points_from_xy(pdf.Longitude, pdf.Latitude))
First, I tried:
gdf['date'] = pd.to_datetime(gdf['date']).dt.date
Tried to export to shapefile using gdf.to_file('sample.shp', driver = "ESRI Shapefile")
, gave me ValueError: Invalid field type <class 'datetime.date'>
Tried to simply convert it to a string, and then export, as suggested in the answer.
gdf['date'] = gdf['date'].dt.strftime('%Y-%m-%d')
but this didn't work either. When I did gdf.info()
it would show that the type was no longer datetime
, but object
instead. And yet it throws ValueError: Invalid field type <class 'datetime.time'>
datetime
no matter what you store in it - so don't do that