4

I am using GeoPandas in the following routine to create the union of 2 (polygon) shapefiles:

import geopandas as gpd

def union(fn_A, fn_B, output):
    shpA = gpd.read_file(fn_A)    
    shpB = gpd.read_file(fn_B)
    union = gpd.overlay(shpA, shpB, how='union')
    union.to_file(driver='ESRI Shapefile', filename=output) 

Shapefile 'A' has an integer attribute "a" and shapefile 'B' has an integer attribute "b". The resulting shapefile has attributes "a" and "b" but of them are doubles. I have read that GeoPandas infers a schema when saving to files, but do not know how to even get the schema of the inputs in the first place using GeoPandas. It appears that this only happens if the result of the union contains empty fields (i.e. areas where 'A' and 'B' don't overlap). If 'A' is completely covering 'B', field a remains integer.

How can I avoid this conversion?

2 Answers 2

4

It is only since 0.24 that Pandas can handle integer columns with Nan and it requires that the columns are cast as a new type Int64 instead of the usual int64. See https://pandas.pydata.org/pandas-docs/version/0.24/whatsnew/v0.24.0.html#optional-integer-na-support. If you convert all your integer columns to Int64 before the overlay it might work.

df['intcol'] = df['intcol'].astype('Int64')
3
  • This sounds good, but I get TypeError: data type not understood. Pandas version 0.24.1, Geopandas 0.4.0, Numpy 1.15.4.
    – styx
    Feb 25, 2019 at 8:55
  • This answers how to convert a particular column to integers, but not the general question which is: how to preserve field types in geopandas
    – jberrio
    Sep 24, 2019 at 8:13
  • I got this error when I had an object in one of the columns. A Shapely Point object in my case, although I wasn't using it for the geometry. I suspect that your error is because the column type is of type "object' of some kind Aug 31, 2020 at 15:21
3

I solved this issue for most of the datatypes within an ESRI Shapefile. Still not quite able to preserve all fields, but this will let you create a Shapefile with 'Short integer' or 'Long integer' format preserved:

'''python'''
## For 'Short integer' format
field = 'field_ab'   ##The combined field, which might contain empty entries
schema = gpd.io.file.infer_schema(geo_df)
schema['properties'][field] = 'int32:4'
geo_df.to_file(driver = 'ESRI Shapefile', filename= output, schema=schema)

## For 'Long integer' format
field = 'field_ab'   ##The combined field, which might contain empty entries
schema = gpd.io.file.infer_schema(geo_df)
schema['properties'][field] = 'int32:10'
geo_df.to_file(driver = 'ESRI Shapefile', filename= output, schema=schema)

Also, see the similar post here: https://stackoverflow.com/questions/62998994/geopandas-export-changing-data-type/68232025#68232025

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.