GeoPandas – Fixing ValueError in Python Spatial Join

geodataframegeopandaspythonspatial-joinvalueerror

I'm working on a spatial join between a polygon from a shapefile and points from a CSV file, using geopandas.sjoin. I want to assign each point with a name of the province it is located in. I do know how to do it in R, but I'm restricted to use Python in this context.
I found an example of a sjoin over here, so I tried it:

My data looks as follows:

import pandas as pd
import geopandas as gp
from shapely.geometry import Point
from geopandas import GeoDataFrame

#My observation points:
obs = pd.read_csv('data/butterflies.csv')
pts = gp.GeoDataFrame(obs, geometry=geopandas.points_from_xy(stations.lon, stations.lat))

pts.head()

enter image description here

#Polygons of some provinces in The Netherlands:
nlpoly = gp.read_file('data/provinces_NL.shp')
nlpoly

enter image description here

Now when I try the spatial join as was suggested in the example above, it returns me a ValueError:

gp.sjoin(pts, nlpoly[['name']],how='left',op='intersects')

>>ValueError: 'right_df' should be GeoDataFrame, got <class 'pandas.core.frame.DataFrame'>

I checked the types for both objects but they were all GeoDataFrames as it should be, yet sjoin does not recognize my rigth_df as a GeoDataFrame.
I guess the bug is in the referencing of the column "name" that I want to assign to the observation points, but I have no clue how to do it right.

Best Answer

If you do nlpoly[['name']] you get only DataFrame with one column name. You have to select geometry column as well to keep GeoDataFrame.

gp.sjoin(pts, nlpoly[['name', 'geometry']], how='left', op='intersects')