Atif
Atif

Reputation: 2210

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

I am getting below error when using geopandas and shapely

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

Below is the code:

#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'

Upvotes: 4

Views: 7797

Answers (2)

ar-siddiqui
ar-siddiqui

Reputation: 170

I got the same error after updating Geopandas from an older version. Following fix did the trick.

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

to

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

Upvotes: 0

Atif
Atif

Reputation: 2210

I am finally able to resolve it by changing this below piece of code

gdf = GeoDataFrame(complete_major_accidents,  geometry)

to

gdf = GeoDataFrame(complete_nonmajor_accidents,  geometry = geometry)

Upvotes: 3

Related Questions