[英]Saving dataframe to shapefile using geopandas in python raises ValueError for bool
当我将数据帧保存为具有几何的shapefile时,我收到以下错误。
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
我无法找出这个错误的含义。
TL; DR :重新生成dtype
bool
到int
列。
这个错误来自这样一个事实,即在设计上,ESRI Shapefile不知道“布尔”数据类型是什么。 它只知道整数是什么 。 大多数人最终做的只是将数据类型更改回整数,即True - > 1和False - > 0。
要找出已为bool
数据类型指定了哪些列,请执行以下操作:
geopandas.io.file.infer_schema(df)
>>> {'geometry': 'Point',
'properties': OrderedDict([('integer', 'int'),
('c', 'bool')])
给定具有bool
类型的列c
的数据帧df
,我会这样做:
df['c'] = df['c'].astype('int')
我们可以编写一个简单的函数来为我们处理所有这些:
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
您还可以查看此问题,如Github上的geopandas repo中所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.