pythonnumpydatashader

Module 'numpy' has no attribute 'warnings'


I'm trying to reproduce this tutorial with my own data. I've a simple square grid of polygons:

from shapely import wkt
import pandas as pd
import geopandas as gpd

data_list = [
    [0,51, wkt.loads("POLYGON ((-74816.7238 5017078.8988, -74716.7238 5017078.8988, -74716.7238 5016978.8988, -74816.7238 5016978.8988, -74816.7238 5017078.8988))")],
    [1,91, wkt.loads("POLYGON ((-74816.7238 5016978.8988, -74716.7238 5016978.8988, -74716.7238 5016878.8988, -74816.7238 5016878.8988, -74816.7238 5016978.8988))")],
    [2,88, wkt.loads("POLYGON ((-74816.7238 5016878.8988, -74716.7238 5016878.8988, -74716.7238 5016778.8988, -74816.7238 5016778.8988, -74816.7238 5016878.8988))")],
    [3,54, wkt.loads("POLYGON ((-74816.7238 5016778.8988, -74716.7238 5016778.8988, -74716.7238 5016678.8988, -74816.7238 5016678.8988, -74816.7238 5016778.8988))")],
    [4,51, wkt.loads("POLYGON ((-74816.7238 5016678.8988, -74716.7238 5016678.8988, -74716.7238 5016578.8988, -74816.7238 5016578.8988, -74816.7238 5016678.8988))")],
]

df = pd.DataFrame(data_list, columns=["id", "data", "geometry"])
gdf = gpd.GeoDataFrame(df, geometry="geometry", crs=32633)

I've translate GeoPandas GeoDataFrame to SpatialPandas Geodataframe:

from spatialpandas import GeoDataFrame

sp_gdf = GeoDataFrame(gdf)

At this point I try to create a choropleth map according to this example:

import datashader as ds

canvas = ds.Canvas(plot_width=1000, plot_height=1000)
agg = canvas.polygons(sp_gdf, 'geometry', agg=ds.mean('data'))

But I'm facing in the error below:

AttributeError                            Traceback (most recent call last)
Cell In[7], line 4
      1 import datashader as ds
      3 canvas = ds.Canvas(plot_width=1000, plot_height=1000)
----> 4 agg = canvas.polygons(sp_gdf, 'geometry', agg=ds.mean('data'))
      6 agg

File ~/.cache/pypoetry/virtualenvs/drakonotebook-larABRfp-py3.10/lib/python3.10/site-packages/datashader/core.py:753, in Canvas.polygons(self, source, geometry, agg)
    751     agg = any_rdn()
    752 glyph = PolygonGeom(geometry)
--> 753 return bypixel(source, self, glyph, agg)

File ~/.cache/pypoetry/virtualenvs/drakonotebook-larABRfp-py3.10/lib/python3.10/site-packages/datashader/core.py:1258, in bypixel(source, canvas, glyph, agg, antialias)
   1255 canvas.validate()
   1257 # All-NaN objects (e.g. chunks of arrays with no data) are valid in Datashader
-> 1258 with np.warnings.catch_warnings():
   1259     np.warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
   1260     return bypixel.pipeline(source, schema, canvas, glyph, agg, antialias=antialias)

File ~/.cache/pypoetry/virtualenvs/drakonotebook-larABRfp-py3.10/lib/python3.10/site-packages/numpy/__init__.py:320, in __getattr__(attr)
    317     from .testing import Tester
    318     return Tester
--> 320 raise AttributeError("module {!r} has no attribute "
    321                      "{!r}".format(__name__, attr))

AttributeError: module 'numpy' has no attribute 'warnings'

I'm on Ubuntu 22.04, with Python 3.10 and the code above runs in a Jupyter Notebook. Below the version of libraries in use:

Moreover the python environment is managed by Poetry 1.4.2

NB: this thread is complete unuseful.


Solution

  • It turns out that numpy.warnings is just a reference to the warnings built-in Python module, as I can see on my NumPy version:

    >>> import numpy as np
    >>> np.__version__
    '1.21.5'
    >>> np.warnings
    <module 'warnings' from 'D:\\Anaconda3\\lib\\warnings.py'>
    

    So, one possible workaround for your problem, may be adding that reference to the NumPy module in your script, manually, at runtime:

    import numpy as np
    import warnings
    np.warnings = warnings
    
    <YOUR SCRIPT HERE>
    

    However, as described in the thread pointed by user @WarrenWeckesser, you may want to update the version of the datashader package, so that package will no longer try to access the np.warnings reference, which turns out to have been removed from numpy.