1

I have a gdf with this characteristics:

gdf.info()

<class 'geopandas.geodataframe.GeoDataFrame'>
Int64Index: 405943 entries, 0 to 405942
Data columns (total 7 columns):
 #   Column    Non-Null Count   Dtype   
---  ------    --------------   -----   
 0   Mz        405943 non-null  object  
 1   Area_km2  405943 non-null  float64 
 2   UBIGEO    405943 non-null  object  
 3   IDPROV    405943 non-null  object  
 4   CCDD      405943 non-null  object  
 5   PobMz     405943 non-null  float64 
 6   geometry  405943 non-null  geometry
dtypes: float64(2), geometry(1), object(4)
memory usage: 40.9+ MB

So, first I want to get centroids from it:

cent=gdf.centroid
cent.head()

0    POINT (546598.404 8580848.223)
1    POINT (537595.600 8588489.910)
2    POINT (534681.734 8586405.771)
3    POINT (558020.684 8571566.834)
4    POINT (535966.243 8592514.133)
dtype: geometry

cent.crs

<Projected CRS: EPSG:32718>
Name: WGS 84 / UTM zone 18S
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: Between 78°W and 72°W, southern hemisphere between 80°S and equator, onshore and offshore. Argentina. Brazil. Chile. Colombia. Ecuador. Peru.
- bounds: (-78.0, -80.0, -72.0, 0.0)
Coordinate Operation:
- name: UTM zone 18S
- method: Transverse Mercator
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

But when I want to do a Spatial Join with another gdf:

trying = gpd.sjoin(cent, blocks, how="left")

I get this message:

ValueError: 'left_df' should be GeoDataFrame, got <class 'geopandas.geoseries.GeoSeries'>

How should be the proper code?

0

1 Answer 1

1

The result of the command cent=gdf.centroid is a GeoSerie and not a GeoDataFrame

gdf = gpd.read_file("poly.shp")
print(gdf)
   id                  geometry
0   1  POLYGON ((48.353 -92.936, 294.389 -111.862, 28...

cent=gdf.centroid
type(cent)
<class 'geopandas.geoseries.GeoSeries'>
 print(cent)
 0    POINT (158.530 -257.815)
dtype: geometry

Solution

cent  = gdf.copy() # copy GeoDataFrame
cent.geometry = gdf.geometry.centroid
type(cent)
 <class 'geopandas.geodataframe.GeoDataFrame'>
print(cent)
     id            geometry
 0    1  POINT (158.530 -257.815)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.