英文:
Module 'numpy' has no attribute 'warnings'
问题
I am your Chinese translator, and here is the translated content you requested:
我正在尝试使用自己的数据重现[此教程][1]。我有一个简单的多边形方格:
```python
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)
我将GeoPandas GeoDataFrame转换为SpatialPandas Geodataframe:
from spatialpandas import GeoDataFrame
sp_gdf = GeoDataFrame(gdf)
此时,我尝试根据此示例创建一个等值线地图:
import datashader as ds
canvas = ds.Canvas(plot_width=1000, plot_height=1000)
agg = canvas.polygons(sp_gdf, 'geometry', agg=ds.mean('data'))
但我遇到了以下错误:
AttributeError: module 'numpy' has no attribute 'warnings'
我使用的是Ubuntu 22.04,Python 3.10,并且以上代码在Jupyter Notebook中运行。以下是使用的库的版本:
- shapely: 2.0.1
- pandas: 2.0.1
- geopandas: 0.12.2
- spatialpandas: 0.4.7
- datashader: 0.14.4
- numpy: 1.24.3
此外,Python环境由Poetry 1.4.2管理。
注意:此 线程完全无用。
<details>
<summary>英文:</summary>
I'm trying to reproduce [this][1] 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][2] 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:
- shapely: 2.0.1
- pandas: 2.0.1
- geopandas: 0.12.2
- spatialpandas: 0.4.7
- datashader: 0.14.4
- numpy: 1.24.3
Moreover the python environment is managed by Poetry 1.4.2
NB: [this][3] thread is complete unuseful.
[1]: https://datashader.org/user_guide/Polygons.html#geopandas-import
[2]: https://datashader.org/user_guide/Polygons.html#plotting-as-filled-polygons
[3]: https://stackoverflow.com/questions/74863592/attributeerror-module-numpy-has-no-attribute-warnings
</details>
# 答案1
**得分**: 2
`numpy.warnings` 只是对内置的 Python `warnings` 模块的引用,如我在我的 NumPy 版本中看到的:
```python
import numpy as np
np.__version__ # '1.21.5'
np.warnings
所以,你的问题可能的一个 解决方法 是在运行时手动将该引用添加到你的脚本中:
import numpy as np
import warnings
np.warnings = warnings
# 在此处添加你的脚本
然而,正如用户 @WarrenWeckesser 指出的线程中所描述的,你可能需要更新 datashader
包的版本,以便该包不再尝试访问 np.warnings
引用,因为这个引用已经从 NumPy 中被移除了。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论