10

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?

joris
  • 133,120
  • 36
  • 247
  • 202
Dmitriy Grankin
  • 568
  • 9
  • 21
  • it's **really** difficult to give any advice without seeing the code that generated the error and ideally a full, but simple, example that reproduces the error. can you build up a minimal example with ~3 or 4 simple geometries in two dataframe and show the code that's generating the error. – Paul H Dec 28 '18 at 15:51

1 Answers1

15

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)

joris
  • 133,120
  • 36
  • 247
  • 202
  • Note that this can typically happen if you run the same join command successively (in debug mode or in a notebook for instance). – CharlesG Feb 27 '23 at 10:50