BUG: crs is lost after calling GeoDataFrame.apply() method
See original GitHub issuecrs 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:
- Created 5 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I honestly thought we fixed this in #1478 but that apparently fixes only
GeoSeries.applynotGeoDataFrame.apply. We should do the similar wrapper for GeoDataFrame as well then.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.
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
_constructorproperly).