GEOPANDAS .sjoin 'index_left' and 'index_right' cannot be names in the frames being joined

Viewed 5572

I am trying to make a spatial join of 2 geo data frames.

Both the indexes are of this kind: RangeIndex(start=0, stop=312, step=1)

I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-228-d0d0190e2940> in <module>()
----> 1 Sales_new_data_concastinate_SR_coundaries_joines=gpd.sjoin(Sales_new_data_concastinated,SR_locations, op='within',how='left')

~/anaconda3/lib/python3.7/site-packages/geopandas/tools/sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix)
     51             or any(right_df.columns.isin([index_left, index_right]))):
     52         raise ValueError("'{0}' and '{1}' cannot be names in the frames being"
---> 53                          " joined".format(index_left, index_right))
     54 
     55     # the rtree spatial index only allows limited (numeric) index types, but an

ValueError: 'index_left' and 'index_right' cannot be names in the frames being joined

the source https://github.com/geopandas/geopandas/blob/master/geopandas/tools/sjoin.py

says:

the rtree spatial index only allows limited (numeric) index types, but an # index in geopandas may be any arbitrary dtype. so reset both indices now # and store references to the original indices, to be reaffixed later. –

What does is what me to do actually?

1 Answers

You probably have columns named 'index_left' and 'index_right' in one of your GeoDataFrames.

This might be annoying for your specific case, but this is how the spatial join method is currently implemented in GeoPandas. So you need to either delete them or rename them.

Solution 1: if you have a column in the left or right dataframe with those names, you can rename this column using the rename method: e.g. df = df.rename(columns={'left_index': 'other_name'}.

Solution 2: delete the columns with df = df.drop(['index_right', 'index_left'], axis=1)

Related