question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: crs is lost after calling GeoDataFrame.apply() method

See original GitHub issue

crs is null on GeoDataFrame.apply(...) result. the following unit test fails because result.crs is None:

    def test_apply_function_to_rows():
        gdf = GeoDataFrame(
            data={'foo': ['Bar']},
            geometry=[Point(45.5089192, -73.5565212)],
            crs={'init': 'epsg:4326'}
        )

        def _lower_foo(row):
            row['foo'] = row['foo'].lower()
            return row

        result = gdf.apply(_lower_foo, axis=1, reduce=False)

        assert_geodataframe_equal(
            result,
            GeoDataFrame(
                data={'foo': ['bar']},
                geometry=[Point(45.5089192, -73.5565212)],
                crs={'init': 'epsg:4326'}
            )
        )

running with the environment below:

$ pip list
Package         Version    
--------------- -----------
atomicwrites    1.2.1      
attrs           18.2.0     
Click           7.0        
click-plugins   1.0.4      
cligj           0.5.0      
Fiona           1.8.4      
geopandas       0.4.0      
more-itertools  5.0.0      
munch           2.3.2      
numpy           1.16.0     
pandas          0.23.4     
pip             19.0.1     
pluggy          0.8.1      
py              1.7.0      
pyproj          1.9.6      
pytest          4.1.1      
python-dateutil 2.7.5      
pytz            2018.9     
setuptools      40.6.3     
Shapely         1.6.4.post2
six             1.12.0     

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
martinfleiscommented, Jan 12, 2021

I honestly thought we fixed this in #1478 but that apparently fixes only GeoSeries.apply not GeoDataFrame.apply. We should do the similar wrapper for GeoDataFrame as well then.

1reaction
jorisvandenbosschecommented, Feb 28, 2021

The original issue about CRS getting lost is fixed in #1848, so closing this issue.

There is one more aspect that @JessicaS11 mentioned, about what object is being passed to the applied function (when applying it to each column, I assume), whether it is a Series or GeoSeries.

In [1]: df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))

In [2]: def func(col):
   ...:     print(type(col))
   ...:     return col
   ...: 

In [3]: df.apply(func)
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
Out[3]: 
       pop_est      continent                      name iso_a3  gdp_md_est                                           geometry
0       920938        Oceania                      Fiji    FJI      8374.0  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1     53950935         Africa                  Tanzania    TZA    150600.0  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2       603253         Africa                 W. Sahara    ESH       906.5  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3     35623680  North America                    Canada    CAN   1674000.0  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4    326625791  North America  United States of America    USA  18560000.0  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...
..         ...            ...                       ...    ...         ...                                                ...
172    7111024         Europe                    Serbia    SRB    101800.0  POLYGON ((18.82982 45.90887, 18.82984 45.90888...
173     642550         Europe                Montenegro    MNE     10610.0  POLYGON ((20.07070 42.58863, 19.80161 42.50009...
174    1895250         Europe                    Kosovo    -99     18490.0  POLYGON ((20.59025 41.85541, 20.52295 42.21787...
175    1218208  North America       Trinidad and Tobago    TTO     43570.0  POLYGON ((-61.68000 10.76000, -61.10500 10.890...
176   13026129         Africa                  S. Sudan    SSD     20880.0  POLYGON ((30.83385 3.50917, 29.95350 4.17370, ...

[177 rows x 6 columns]

So the last column is also passed as a Series and not as GeoSeries.

I am not sure that is easy to fix (override) on the GeoPandas side, though. Should take a look at how this is implemented in pandas (eg it might not be using _constructor properly).

Read more comments on GitHub >

github_iconTop Results From Across the Web

geopandas.GeoDataFrame
A GeoDataFrame object is a pandas.DataFrame that has a column with geometry. In addition to the standard DataFrame constructor arguments, GeoDataFrame also ...
Read more >
Merging geodataframes in geopandas (CRS do not match)
You can call mygeodataframe.crs to find the CRS of each dataframe, and then to_crs() to reproject one to match the other, like so:...
Read more >
How do I correctly reproject a geodataframe with multiple ...
My workaround is to not use a GeoDataFrame , but rather combine a normal pandas DataFrame , for the non-shapely data, with several...
Read more >
Convert foreign object to an sf object — st_as_sf • sf - r-spatial
S3 method for s2_geography st_as_sf(x, ..., crs = st_crs(4326)) ... logical; if TRUE , raise an error if coordinates contain missing values. sf_column_name....
Read more >
Geopandas Basics using Open Data
This spatial data has a coordinate reference system (CRS), ... shp_gdf.tail() #look at geodataframe, geometry column contains spatial coordinate data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found