question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError on setting a geometry to a multipolygon ("equal len keys and value")

See original GitHub issue

I 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:

MULTIPOLYGON.txt

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
elizabethswkimcommented, Mar 9, 2020

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

2reactions
martinfleiscommented, May 12, 2020

Just posting another workaround here. Go through single element GeoSeries. Might be helpful for some cases.

geom = df.loc[1, 'geometry']
df.loc[[0], 'geometry'] = geopandas.GeoSeries([geom])

~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.~

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found