4

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'
7
  • can you always include the full traceback when asking about errors? they include tons of important debugging info, such as exactly where the error was caused and how. Check out the How to Ask guide for more guidance. thanks! Commented Jun 30, 2022 at 16:33
  • also - what's going on here? gdf.plot(ax = ax.plot(...))? and can you indent your code correctly? whitespace is really important in python - it's hard enough for us to read someone else's code when it's formatted correctly :) Commented Jun 30, 2022 at 16:35
  • I have formatted the code.
    – Atif
    Commented Jun 30, 2022 at 18:34
  • the code in the traceback is different from the code in your example. can you make sure that the code runs straight through and that you post the exact code you ran to generate the error? Commented Jun 30, 2022 at 19:36
  • but more generally, you're calling plotting functions inside other plotting functions. why are you doing this? gdf.plot(ax = ax.plot(...))? what are you trying to do here? what happens if you just do gdf.plot("column name", **plotting_kwargs), e.g. gdf.plot(marker='o', color='red', markersize=15, edgecolor='black', ax=ax)? Commented Jun 30, 2022 at 19:40

2 Answers 2

3

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)
0

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))

1
  • Note: list obvs cannot be empty Commented Nov 3, 2022 at 11:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.