3

I'm trying to set the coordinate reference system for a GeoDataFrame using the following code:

import geopandas
from shapely.geometry import shape
from shapely.geometry import Point

d = {'col1': ['name1', 'name2'], 'geometry': [Point(1, 2), Point(2, 1)]}
gdf = geopandas.GeoDataFrame(d)

gdf = gdf.set_crs('epsg:3857')

However, when running gdf.set_crs('epsg:3857'), I receive the following error:

File "C:\Users\username\AppData\Local\Programs\Python\Python39\my_env\lib\site-packages\pandas\core\generic.py", line 5487, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'GeoDataFrame' object has no attribute 'set_crs'

I looked inside geodataframe.py and indeed there is no object called set_crs. How do I get the set_crs object in here? FYI I installed GeoPandas from https://www.lfd.uci.edu/~gohlke/pythonlibs/.


I tried copying the set_crs object (found here: https://github.com/geopandas/geopandas/blob/528abfc6fe4a501ebffa7aa16cb05a5aa656c8dd/geopandas/geodataframe.py#L1116) into geodataframe.py, but it did not solve the issue. Instead I received more errors.

4
  • 1
    Try just using the integer 3857. See geopandas.org/docs/reference/api/… . Ie gdf = gdf.set_crs(3857, allow_override=True). Also, I would definitely install the package from source rather than someone's unofficial release if you're having an issue with a core attribute
    – Encomium
    Nov 2, 2021 at 22:48
  • 2
    The set_crs method was added in geopandas 0.8.0. There's two versions on lfd.uci.edu/~gohlke/pythonlibs, did you install the old 0.6.2 version instead of 0.10.2?
    – user2856
    Nov 3, 2021 at 1:08
  • @user2856 is correct! In my case this gdf.set_crs('epsg:3857') works without problems, I am on GeoPandas 0.10.2.
    – Taras
    Nov 3, 2021 at 8:19
  • Why not add the CRS when you invoke the constructor?: gdf = geopandas.GeoDataFrame(d, crs="EPSG:3857")
    – Dobedani
    Dec 7, 2022 at 18:22

0