Cannot write csv in 0.6.0 if geometry contains multipolygon
See original GitHub issueI have encountered an error in 0.6.0 when writing a GeoDataFrame to csv if the geometry has a MultiPolygon shape.
This worked in 0.5.1:
$ conda create -n gpd051 python=3.6 geopandas=0.5.1
<conda output>
$ conda activate gpd051
(gpd051) $ python
Python 3.6.7 | packaged by conda-forge | (default, Jul 2 2019, 02:07:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from shapely.wkt import loads
>>> import geopandas as gpd
>>> geom_str = 'MULTIPOLYGON (((1 0, 1 1, 0 1, 0 0, 1 0)), ((3 0, 3 1, 2 1, 2 0, 3 0)))'
>>> geom = loads(geom_str)
>>> gdf = gpd.GeoDataFrame(geometry=[geom])
>>> gdf.to_csv()
',geometry\n0,"MULTIPOLYGON (((1 0, 1 1, 0 1, 0 0, 1 0)), ((3 0, 3 1, 2 1, 2 0, 3 0)))"\n'
But the same steps fail in 0.6.0 with ValueError: setting an array element with a sequence
:
$ conda create -n gpd060 python=3.6 geopandas=0.6.0
<conda output>
$ conda activate gpd060
(gpd060) $ python
Python 3.6.7 | packaged by conda-forge | (default, Jul 2 2019, 02:07:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from shapely.wkt import loads
>>> import geopandas as gpd
>>> geom_str = 'MULTIPOLYGON (((1 0, 1 1, 0 1, 0 0, 1 0)), ((3 0, 3 1, 2 1, 2 0, 3 0)))'
>>> geom = loads(geom_str)
>>> gdf = gpd.GeoDataFrame(geometry=[geom])
>>> gdf.to_csv()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/john.flavin/anaconda3/envs/gpd060/lib/python3.6/site-packages/pandas/core/generic.py", line 3228, in to_csv
formatter.save()
File "/Users/john.flavin/anaconda3/envs/gpd060/lib/python3.6/site-packages/pandas/io/formats/csvs.py", line 202, in save
self._save()
File "/Users/john.flavin/anaconda3/envs/gpd060/lib/python3.6/site-packages/pandas/io/formats/csvs.py", line 324, in _save
self._save_chunk(start_i, end_i)
File "/Users/john.flavin/anaconda3/envs/gpd060/lib/python3.6/site-packages/pandas/io/formats/csvs.py", line 340, in _save_chunk
quoting=self.quoting,
File "/Users/john.flavin/anaconda3/envs/gpd060/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 760, in to_native_types
values = values.astype(str)
ValueError: setting an array element with a sequence
I have a hypothesis about what’s going on. (I don’t know much about the internals of how GeoDataFrames are represented, so take this with a grain of salt.) The source of the exception is this block in pandas/core/internals/blocks.py
:
if not self.is_object and not quoting:
values = values.astype(str)
else:
values = np.array(values, dtype="object")
My guess is that since the geometry
column used to be an object
type, not self.is_object
would have been False
and values = values.astype(str)
would not have been evaluated. But now geometry
is of type geometry
so not self.is_object
is True
and we get the error.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Geopandas: how to read a csv and convert to ... - Stack Overflow
Geopandas seems to be unable to convert a geometry column from a pandas dataframe. Solution number 2. Try applying the shapely wkt.loads ...
Read more >sf.pdf
If prepared is TRUE, and x contains POINT geometries and y contains polygons, then the polygon geometries are prepared, rather than the points....
Read more >TypeError: Input geometry column must contain valid ...
I'm working in a urban biodiversity project and I have a csv file for a birds inventory with a column ...
Read more >Package 'sf'
x an object of class sf, sfc or sfg that has mixed geometry (GEOMETRY or GEOMETRYCOLLECTION). type character; one of "POLYGON", "POINT", "LINESTRING"...
Read more >Data Import : : CHEAT SHEET
write_file(x = "a,b,c\n1,2,3\n4,5,NA", path = "file.csv"). Semi-colon Delimited Files ... Expand Tables- quickly create tables with combinations of values.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@johnflavin I opened a PR to fix the issue on the pandas side (https://github.com/pandas-dev/pandas/pull/28841), and a PR in geopandas to fix the astype issue (https://github.com/geopandas/geopandas/pull/1147).
This means that for your exact example to work, you will need to wait until the next pandas release (or stay on geopandas 0.5). But with the fix in geopandas (to be released in 0.6.1), you will already be able to work-around it with first casting your geometry column to strings before calling
to_csv
:@johnflavin FYI 0.6.1 is released