4

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 '21 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 '21 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 '21 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 '21 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 '21 at 17:20
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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