user4602735
user4602735

Reputation:

Saving dataframe to shapefile using geopandas in python raises ValueError for bool

When I am saving the dataframe as a shapefile with geometry I am getting the following error.

geometry  = [Point(xy) for xy in zip(df.longitude, df.latitude)]
dfout = geopandas.GeoDataFrame(df,  geometry=geometry)    
dfout.to_file(outputpath, driver='ESRI Shapefile')

Traceback (most recent call last):
  File "test.py", line 230, in <module>
    main()
  File "test.py", line 223, in main
    createSHP(df,outputpath)
  File "test.py", line 150, in createSHP
    dfout.to_file(outputpath, driver='ESRI Shapefile')
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/geopandas/geodataframe.py", line 343, in to_file
    to_file(self, filename, driver, schema, **kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/geopandas/io/file.py", line 61, in to_file
    schema=schema, **kwargs) as c:
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/fiona/__init__.py", line 178, in open
    enabled_drivers=enabled_drivers, crs_wkt=crs_wkt)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/fiona/collection.py", line 155, in __init__
    self.session.start(self, **kwargs)
  File "fiona/ogrext.pyx", line 961, in fiona.ogrext.WritingSession.start (fiona/ogrext.c:16015)
ValueError: 'bool' is not in list

I havn't been able to find out what this error means.

Upvotes: 3

Views: 4839

Answers (1)

Fran&#231;ois Leblanc
Fran&#231;ois Leblanc

Reputation: 1750

TL;DR: Recast columns with dtype bool to int.


This error comes from the fact that by design, the ESRI Shapefile doesn't know what a "boolean" data type is. It just knows what integers are instead. What most people end up doing is simply to change the datatype back to integer, i.e. True -> 1 and False -> 0.

To find out what column(s) have been assigned a bool datatype, go with:

geopandas.io.file.infer_schema(df)

>>> {'geometry': 'Point',
     'properties': OrderedDict([('integer', 'int'),
                                ('c', 'bool')])

Given dataframe df with a column c of type bool, I'd do:

df['c'] = df['c'].astype('int')

We can write a simple function that takes care of all this for us:

def gdf_bool_to_int(gdf):
    """For a given GeoDataFrame, returns a copy that
    recasts all `bool`-type columns as `int`.

    GeoDataFrame -> GeoDataFrame"""
    df = gdf.copy()
    coltypes = gpd.io.file.infer_schema(df)['properties']
    for colname, coltype in coltypes.items():
        if coltype == 'bool':
            df[colname] = df[colname].astype('int')
    return df

You can also take a look at this issue, as discussed in the geopandas repo on Github.

Upvotes: 3

Related Questions