7

Reading a polygon shapefile with geopandas I got a strange error. I found out it is a normally hidden warning about another error, which I saw only because I turned warnings to errors. It also appears when creating a GeoDataFrame:

import geopandas as gpd
from shapely.geometry import Polygon
import random

import warnings
warnings.filterwarnings('error')

geoms = [Polygon(
    (random.random(), random.random())
        for p in range(10)) for id in range(10)]
gdf = gpd.GeoDataFrame(geometry=geoms)

Output:

Traceback (most recent call last):
  File "C:\Daten2\Anaconda\lib\site-packages\shapely\geometry\polygon.py", line 300, in __array_interface__
    raise NotImplementedError(
NotImplementedError: A polygon does not itself provide the array interface. Its rings do.

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

Traceback (most recent call last):
  File "error.py", line 11, in <module>
    gdf = gpd.GeoDataFrame(geometry=geoms)
  File "C:\Daten2\Anaconda\lib\site-packages\geopandas\geodataframe.py", line 167, in __init__
    self.set_geometry(geometry, inplace=True)
  File "C:\Daten2\Anaconda\lib\site-packages\geopandas\geodataframe.py", line 295, in set_geometry
    level = _ensure_geometry(level, crs=crs)
  File "C:\Daten2\Anaconda\lib\site-packages\geopandas\geodataframe.py", line 43, in _ensure_geometry
    out = from_shapely(data, crs=crs)
  File "C:\Daten2\Anaconda\lib\site-packages\geopandas\array.py", line 168, in from_shapely
    return GeometryArray(vectorized.from_shapely(data), crs=crs)
  File "C:\Daten2\Anaconda\lib\site-packages\geopandas\_vectorized.py", line 142, in from_shapely
    aout[:] = out
DeprecationWarning: An exception was ignored while fetching the attribute `__array_interface__` from an object of type 'Polygon'.  With the exception of `AttributeError` NumPy will always raise this exception in the future.  Raise this deprecation warning to see the original exception. (Warning added NumPy 1.21)

The warning occurs in the line

aout[:] = out

of a geopandas module. out is a list of shapely.geometry.polygon.Polygon, while aout is a numpy array of the same length with dtype object, containing Nones.

So I'm curious what is happening here?

8
  • Show us more code. What does the function pm does? Excerpt plz. Your issue may be Python scope related. Very difficult or impossible to guess from current provided infos as too incomplete
    – ThomasG77
    Jul 1, 2021 at 15:20
  • 2
    Based on your comment 'The same instruction, same file, typed at the python prompt successfully..' leads me to suspect you may have multiple python environments installed ..and the interpreter you're entering manually is somehow different from the interpreter your entering via your scripted solution.
    – elrobis
    Jul 1, 2021 at 16:03
  • 1
    You might try this in both your Python prompt and your script and compare the output: import sys (then next line) print(sys.executable) ..if they give you different results, it means you need to specify the interpreter you're using in your scripted environment.
    – elrobis
    Jul 1, 2021 at 16:07
  • @ThomasG77 pdb.pm() is the post mortem debugger from the standard library. Yes, I will try to create a minimum reproducible program, but I fear that's no easy task.
    – Redoute
    Jul 1, 2021 at 16:58
  • @elrobis I call both prompt and program from the conda command prompt with activated base environment. The program with cd C:\Daten2\G991 python -i g991edat.py. I checked sys.executable, sys.path and os.environment and can't detect any difference.
    – Redoute
    Jul 1, 2021 at 17:20

2 Answers 2

6

This is not an error on user side. It is the result of a recent change in Numpy 1.21 coupled with a way shapely implements (rather not implements) __array_interface__. There is nothing the user should do, it will be fixed in the next release of shapely (see https://github.com/Toblerity/Shapely/pull/1174). You can silence these warnings if you want and keep an eye on the issue.

3
  • I updated shapely and the warning was gone! Thanks for the constructive discussion! Apr 27, 2022 at 7:29
  • 1
    i was trying to follow the thread you posted but am still confused. i think my issue and this one are related but not the same. i'm also getting the same warning when creating a geodataframe with geometry = geopandas.points_from_xy(xx, yy) [i guess more accurately the issue is the points_from_xy call?]. i get the warning both where xx and yy are pandas geoseries or numpy arrays. but i'm not using numpy 1.21 (using np 1.19.5; shapely 1.8; geopandas 0.9). any ideas?
    – user20408
    Jun 1, 2022 at 23:31
  • 1
    @user20408 I would first try using the latest version of geopandas (0.10.2). Jun 2, 2022 at 8:46
0

I also encountered this error. I used conda install -c conda-forge shapely to install shapely but it only gave me 1.7.1 version. So I upgraded it manually pip install Shapely==1.8.2 and the error disappeared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.