[GIS] Perform sjoin in geopandas leads to:’AttributeError: ‘GeoSeries’ object has no attribute ‘columns”

geopandasshapely

I try to find all the polygons in a dataframe thant intersect an other dataframe (made by buffering my polygons) usign sjoin.

First I did the buffers

gdf['geom_buf'] = gdf.geom.buffer(100) 

Then I merged all overlapping polygons, then I transformed it into a geodataframe and exploded the geodataframe

union_gdf = cascaded_union(gdf['geom_buf']) +
union_gdf = gpd.GeoDataFrame(geometry=[union_gdf ])
exp_union_gdf = union_gdf.explode()

Then I tried to find all the buffer that the entities inside gdf intersect

results-intersect = gpd.sjoin(gdf["geom"], exp_union_gdf , how="inner", op='intersects')

I get this error

~\Anaconda3\lib\site-packages\geopandas\tools\sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix)
     46 
     47     # due to GH 352
---> 48     if (any(left_df.columns.isin([index_left, index_right]))
     49             or any(right_df.columns.isin([index_left, index_right]))):
     50         raise ValueError("'{0}' and '{1}' cannot be names in the frames being"

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   4370         if (name in self._internal_names_set or name in self._metadata or
   4371                 name in self._accessors):
-> 4372             return object.__getattribute__(self, name)
   4373         else:
   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):

AttributeError: 'GeoSeries' object has no attribute 'columns'

I guess that exp_union_gdf is not a geodf but a geoseries.

Best Answer

geopandas.sjoin expects a GeoDataFrame, not a GeoSeries. So instead of

gpd.sjoin(gdf["geom"], exp_union_gdf , how="inner", op='intersects')

you can do

gpd.sjoin(gdf, exp_union_gdf , how="inner", op='intersects')