ValueError on setting a geometry to a multipolygon ("equal len keys and value")
See original GitHub issueI have 2 geodataframes with a geometry column and I copy some geometries from 1 to the other. This works well with Polygons but returns a ValueError for any (valid) MultiPolygon.
geodata01.loc[index, 'geometry'] = geodata02.loc[index, 'geometry']
This works well with Polygons. Only with MultiPolygons I get:
ValueError
if len(labels) != len(value):
--> 611 raise ValueError('Must have equal len keys and value '
612 'when setting with an iterable')
I also cannot do assignments of a buffered or simplified MultiPolygon either (the MultiPolygon is valid and I can plot, buffer, simplify but I cannot assign it):
geodata01.loc[index, 'geometry'] = geodata01.loc[index, 'geometry'].buffer(0)
#or
geodata01.loc[index, 'geometry'] = geodata01.loc[index, 'geometry'].simplify(tolerance=0)
This returns the same ValueError. I have tried many geometries and (only) encountered this with (all) multipolygons that I tried. Attached an example of a multipolygon that yields the error:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:6 (4 by maintainers)
Top Results From Across the Web
ValueError for MultiPolygon "equal len keys and value" - Stack ...
Explanation and workaround from the github issue as provided by Joris: "The reason is that pandas checks the length of the value you...
Read more >'Must have equal len keys and value when setting with an ...
I got similar error when the geometry I try to assign is a "MultiPolygon", instead of a "Polygon". Maybe only tile 67 is...
Read more >ValueError on setting a geometry to a multipolygon ("equal len keys ...
I have 2 geodataframes with a geometry column and I copy some geometries from 1 to the other. This works well with Polygons...
Read more >ValueError for MultiPolygon "equal len keys and value"-pandas
I have 2 geodataframes with a geometry column and I copy some geometries from 1 to the other. This works well with Polygons...
Read more >GEOS API - Django documentation
The first is to simply instantiate the object on some spatial input – the following are examples of creating the same geometry from...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
Thank you so much for raising this, @wbijster! And for your responses, @jorisvandenbossche! I couldn’t make this work by assigning to a list of the scalar element, nor could I by indexing on a mask matching the scalar element. I was only able to get this assignment to work using @koshy1123’s solution at @jorisvandenbossche’s follow-up pandas issue, specifically:
gdf.loc[scalar_index_loc, 'geometry'] = geopandas.GeoDataFrame(geometry=[shapely_geo]).geometry.values
Just posting another workaround here. Go through single element
GeoSeries
. Might be helpful for some cases.~If I am correct, this bug could be in future resolved by Shapely 2.0, which makes geometries non-iterable by default - https://github.com/shapely/shapely-rfc/pull/1.~