4

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 enter image description here. 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'>

6
  • see this answer - gis.stackexchange.com/a/267419/79
    – Ian Turton
    Commented Jul 13, 2022 at 14:23
  • @IanTurton that's for both date and time, my date only has date
    – parchinkos
    Commented Jul 13, 2022 at 14:37
  • it's a datetime no matter what you store in it - so don't do that
    – Ian Turton
    Commented Jul 13, 2022 at 14:45
  • @IanTurton How do I change the type? Tried the methods listed here (gis.stackexchange.com/questions/281895/…) but none of them work, and give me a value error instead
    – parchinkos
    Commented Jul 13, 2022 at 14:47
  • 1
    you need to edit your question to show what you've tried and what went wrong
    – Ian Turton
    Commented Jul 13, 2022 at 14:56

1 Answer 1

2

Convert the datetimes to strings/objects.

I have a datetime field called date:

import geopandas as gpd

df = gpd.read_file('/home/bera/GIS/Data/lämningar_sverige.gpkg', layer='lämningar_sverige_point')
df['date'] = df['date'].dt.strftime('%Y-%m-%d') #Convert the datetimes to strings
df.to_file('/home/bera/Desktop/GIStest/datetimes.shp')
5
  • 1
    thank you! i'll do this for now, at least until i find a way to keep it only as a date
    – parchinkos
    Commented Jul 13, 2022 at 17:12
  • Not possible as far as I know. Dates as stored as mm/dd/yyyy hh:mm:ss, with a specification of AM or PM.
    – Bera
    Commented Jul 13, 2022 at 17:20
  • oh okay. unfortunately when i do this i get ValueError: Invalid field type <class 'datetime.time'>. Does it work completely fine for you?
    – parchinkos
    Commented Jul 13, 2022 at 18:00
  • Then you havent converted the datetime to string. Add a sample of your excel file as text to your question and the code you are using to create the geodataframe, and the code you are using to create the datetimes and convert to text and export to shapefile
    – Bera
    Commented Jul 13, 2022 at 18:07
  • edited the original question, not much more relevant in the code now besides what I've written
    – parchinkos
    Commented Jul 13, 2022 at 19:03

Your Answer

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

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