'saving coordinates from Dataframe as Polygons (shapely.geometry) AttributeError

I want to create a Polygon from a list of coordinates:

import pandas as pd
from shapely.geometry import Point, Polygon

data = pd.read_csv('path.csv', sep=';')

the data is in the following format

Suburb features_geometry_x features_geometry_y
1 50.941840 6.9595637
1 50.941845 6.9595698
3 50.94182 6.9595632
4 50.9418837 6.9595958

with several rows for suburb 1, 3 and 4

#create a polygon


I = data.loc[data['Suburb'] == 1]
I['coordinates'] = list(zip(I['features_geometry_x'], I['features_geometry_y']))
poly_i = Polygon(I['coordinates'])

the code above works fine but if I do the same thing for suburb 3 and 4 it yields the following error:

L = data.loc[data['Suburb'] == 3]
L['coordinates'] = list(zip(L['features_geometry_x'], L['features_geometry_y']))
poly_l = Polygon(L['coordinates'])

File "shapely/speedups/_speedups.pyx", line 252, in shapely.speedups._speedups.geos_linearring_from_py

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py", line 5487, in getattr return object.getattribute(self, name)

AttributeError: 'Series' object has no attribute 'array_interface'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3361, in get_loc return self._engine.get_loc(casted_key)

File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item

File "pandas/_libs/hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item

KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File "/var/folders/j6/wgg72kmx145f3krf14nzjfq40000gn/T/ipykernel_4092/214655495.py", line 3, in poly_l = Polygon(Lindenthal['coordinates'])

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/shapely/geometry/polygon.py", line 261, in init ret = geos_polygon_from_py(shell, holes)

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/shapely/geometry/polygon.py", line 539, in geos_polygon_from_py ret = geos_linearring_from_py(shell)

File "shapely/speedups/_speedups.pyx", line 344, in shapely.speedups._speedups.geos_linearring_from_py

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py", line 942, in getitem return self._get_value(key)

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py", line 1051, in _get_value loc = self.index.get_loc(label)

File "/Users/Jojo/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc raise KeyError(key) from err

KeyError: 0

Please help :)



Solution 1:[1]

I think the issue here is that you need more than one data point to create a polygon where as your suburb 2 and 3 each got only a single point.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SW84