2

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?

3

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')
  • 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 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 at 8:13

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.