繁体   English   中英

AttributeError: 'DataFrame' object 在使用 geopandas 时没有属性 'crs'

[英]AttributeError: 'DataFrame' object has no attribute 'crs' when using geopandas

我在使用 geopandas 和 shapely 时遇到错误

AttributeError: 'DataFrame' object has no attribute 'crs'

下面是代码:

#geometry = [Point(xy) for xy in zip(complete_major_accidents['longitude'], complete_major_accidents['latitude'])]
#crs='none'
geometry = gpd.points_from_xy(complete_nonmajor_accidents.longitude, complete_nonmajor_accidents.latitude)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
#geometries = world['geometry'].apply(lambda x: x.wkt).values
#print(geometries)
#print(tuple(geometry))
gdf = GeoDataFrame(complete_major_accidents,  geometry)
gdf


ax = world[world['name'] == 'United Kingdom'].plot(figsize=(15, 15))
#print(type(ax))
gdf.plot(ax = ax, marker='o', color='red', markersize=15, edgecolor='black')
#gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_330/1106976374.py in <module>
     12 ax = world[world['name'] == 'United Kingdom'].plot(figsize=(15, 15))
     13 #print(type(ax))
---> 14 gdf.plot(ax = ax, marker='o', color='red', markersize=15, edgecolor='black')
     15 #gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15)

~/.local/lib/python3.8/site-packages/geopandas/plotting.py in __call__(self, *args, **kwargs)
    961         kind = kwargs.pop("kind", "geo")
    962         if kind == "geo":
--> 963             return plot_dataframe(data, *args, **kwargs)
    964         if kind in self._pandas_kinds:
    965             # Access pandas plots

~/.local/lib/python3.8/site-packages/geopandas/plotting.py in plot_dataframe(df, column, cmap, color, ax, cax, categorical, legend, scheme, k, vmin, vmax, markersize, figsize, legend_kwds, categories, classification_kwds, missing_kwds, aspect, **style_kwds)
    674 
    675     if aspect == "auto":
--> 676         if df.crs and df.crs.is_geographic:
    677             bounds = df.total_bounds
    678             y_coord = np.mean([bounds[1], bounds[3]])

~/.local/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5573         ):
   5574             return self[name]
-> 5575         return object.__getattribute__(self, name)
   5576 
   5577     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'crs'

我终于可以通过更改下面的代码来解决它

gdf = GeoDataFrame(complete_major_accidents,  geometry)

gdf = GeoDataFrame(complete_nonmajor_accidents,  geometry = geometry)

从旧版本更新 Geopandas 后,我遇到了同样的错误。 以下修复成功了。

self.ax = gpd.GeoDataFrame().plot(figsize=(18, 12))

self.ax = gpd.GeoDataFrame(geometry=[]).plot(figsize=(18, 12))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM