Why is my geopandas installation missing sjoin? AttributeError: 'GeoDataFrame' object has no attribute 'sjoin'
Asked Answered
C

1

9

I need to use geopandas sjoin on a project. I have successfully created GeoDataFrame objects and used class methods readfile, crs and to_crs. However when it comes to using sjoin I am getting an error: AttributeError: 'GeoDataFrame' object has no attribute 'sjoin'.

So I thought I would attempt to get sjoin working on a simple example taken directly from the official geopandas website: https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.sjoin.html

import geopandas

countries = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
cities = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
print(countries.head())
print(cities.head())

cities_w_country_data = cities.sjoin(countries)
print(cities_w_country_data.head())

Yet I am encountering the same problem. Please could someone explain this error and offer a solution? Thanks.

The output is as follows:

C:\Users\mmm\AppData\Local\ESRI\conda\envs\arcgispro-py3_clone\lib\site-packages\geopandas\array.py:85: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
  aout[:] = out
C:\Users\mmm\AppData\Local\ESRI\conda\envs\arcgispro-py3_clone\lib\site-packages\geopandas\geodataframe.py:35: ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead.
  out = from_shapely(data)
     pop_est      continent  ...  gdp_md_est                                           geometry
0     920938        Oceania  ...      8374.0  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1   53950935         Africa  ...    150600.0  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2     603253         Africa  ...       906.5  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3   35623680  North America  ...   1674000.0  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4  326625791  North America  ...  18560000.0  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...

[5 rows x 6 columns]
           name                   geometry
0  Vatican City  POINT (12.45339 41.90328)
1    San Marino  POINT (12.44177 43.93610)
2         Vaduz   POINT (9.51667 47.13372)
3    Luxembourg   POINT (6.13000 49.61166)
4       Palikir  POINT (158.14997 6.91664)
Traceback (most recent call last):
  File "geopandas_sjoin_official_eg.py", line 8, in <module>
    cities_w_country_data = cities.sjoin(countries)
  File "C:\Users\mmm\AppData\Local\ESRI\conda\envs\arcgispro-py3_clone\lib\site-packages\pandas\core\generic.py", line 5465, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'GeoDataFrame' object has no attribute 'sjoin'

Note: I am using Windows 10. I am running my code from within a cloned ArcGIS Pro Conda environment. I recently installed geopandas using the command "conda install geopandas -y"

Casaleggio answered 17/12, 2021 at 12:28 Comment(4)
old version of geopandas.... github.com/geopandas/geopandas/releases requires v0.10.0+Strongminded
note that geopandas.sjoin has been around longer and has a similar API, so you can probably use that.Tymothy
Thanks guys - very helpful. The version of geopandas installed using "conda install geopandas -y" from within cloned ArcGIS Pro (v2.9) conda environment is only 0.9.0. I have now rewritten the example above using geopandas.sjoin and it works as expected (see geopandas.org/en/stable/docs/reference/api/geopandas.sjoin.html). I was confusing geopandas.sjoin with geopandas.GeoDataFrame.sjoin.Casaleggio
HAve you tried to import it from geopandas.tools import sjoin and then do your hing: cities_w_country_data = sjoin(cities, countries, how="inner", op='intersects') Simplex
S
0

Import sjoin from geopandas.tools. With your data:

import geopandas
from geopandas.tools import sjoin

countries = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
cities = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
print(countries.head())
print(cities.head())

cities_w_country_data = cities.sjoin(countries)
print(cities_w_country_data.head())

which is

     pop_est      continent                      name iso_a3  gdp_md_est  \
0     920938        Oceania                      Fiji    FJI      8374.0   
1   53950935         Africa                  Tanzania    TZA    150600.0   
2     603253         Africa                 W. Sahara    ESH       906.5   
3   35623680  North America                    Canada    CAN   1674000.0   
4  326625791  North America  United States of America    USA  18560000.0   

                                            geometry  
0  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...  
1  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...  
2  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...  
3  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...  
4  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...  
           name                   geometry
0  Vatican City  POINT (12.45339 41.90328)
1    San Marino  POINT (12.44177 43.93610)
2         Vaduz   POINT (9.51667 47.13372)
3    Luxembourg   POINT (6.13000 49.61166)
4       Palikir  POINT (158.14997 6.91664)
        name_left                   geometry  index_right   pop_est continent  \
0    Vatican City  POINT (12.45339 41.90328)          141  62137802    Europe   
1      San Marino  POINT (12.44177 43.93610)          141  62137802    Europe   
192          Rome  POINT (12.48131 41.89790)          141  62137802    Europe   
2           Vaduz   POINT (9.51667 47.13372)          114   8754413    Europe   
184        Vienna  POINT (16.36469 48.20196)          114   8754413    Europe   

    name_right iso_a3  gdp_md_est  
0        Italy    ITA   2221000.0  
1        Italy    ITA   2221000.0  
192      Italy    ITA   2221000.0  
2      Austria    AUT    416600.0  
184    Austria    AUT    416600.0  

If you want another type of join

cities_w_country_data = sjoin(cities, countries, how="inner", op='intersects')

which gives

        name_left                     geometry  index_right    pop_est  \
0    Vatican City    POINT (12.45339 41.90328)          141   62137802   
1      San Marino    POINT (12.44177 43.93610)          141   62137802   
192          Rome    POINT (12.48131 41.89790)          141   62137802   
2           Vaduz     POINT (9.51667 47.13372)          114    8754413   
184        Vienna    POINT (16.36469 48.20196)          114    8754413   
..            ...                          ...          ...        ...   
195       Jakarta   POINT (106.82749 -6.17247)            8  260580739   
196        Bogota    POINT (-74.08529 4.59837)           32   47698524   
197         Cairo    POINT (31.24802 30.05191)          163   97041072   
198         Tokyo   POINT (139.74946 35.68696)          155  126451398   
200      Santiago  POINT (-70.66899 -33.44807)           10   17789267   

         continent name_right iso_a3  gdp_md_est  
0           Europe      Italy    ITA   2221000.0  
1           Europe      Italy    ITA   2221000.0  
192         Europe      Italy    ITA   2221000.0  
2           Europe    Austria    AUT    416600.0  
184         Europe    Austria    AUT    416600.0  
..             ...        ...    ...         ...  
195           Asia  Indonesia    IDN   3028000.0  
196  South America   Colombia    COL    688000.0  
197         Africa      Egypt    EGY   1105000.0  
198           Asia      Japan    JPN   4932000.0  
200  South America      Chile    CHL    436100.0  

[172 rows x 8 columns]
Simplex answered 24/4, 2023 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.