BUG: Losing the geometry column should convert to a DataFrame
See original GitHub issueBranched out from the discussion in #1653
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of geopandas.
-
(optional) I have confirmed this bug exists on the master branch of geopandas.
Code Sample, a copy-pastable example
From @jorisvandenbossche 's comment in #1653
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
type(df.drop("geometry", axis=1)) # geopandas.geodataframe.GeoDataFrame
type(df.reindex(columns=["name"])) # geopandas.geodataframe.GeoDataFrame
Problem description
When a GeoDataFrame loses its geometry column (via .drop()
or .reindex()
, for instance), the result should be a regular pandas DataFrame. This makes intuitive sense and would allow easier compatibility with the pandas API, as well as preventing issues such as the one found in #1653.
Right now, these methods are simply inherited from the DataFrame class but we can overwrite them in the GeoDataFrame to automate this downcasting when possible.
Expected Output
type(df.drop("geometry", axis=1)) # pandas.core.frame.DataFrame
type(df.reindex(columns=["name"])) # pandas.core.frame.DataFrame
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Should dropping the geometry column produce a Pandas ...
Yes, it seems to make sense to me to return a DataFrame in those occasions, as it would otherwise be an invalid GeoDataFrame...
Read more >python - geopandas GeoDataFrame gets converted to pandas ...
When I try to set data types for geoDataFrame columns, it returns a pandas DataFrame. Is there a way to return a GeoDataFrame...
Read more >How to create a SpatiallyEnabledDataFrame from an
All I want to do is create a spatially enable dataframe from an existing dataframe with a SHAPE column containing the respective arcpy...
Read more >pandas.DataFrame.stack — pandas 1.5.2 documentation
Stacking a column level onto the index axis can create combinations of index and column values that are missing from the original dataframe....
Read more >Changelog — GeoPandas 0.12.2+0.gefcb367.dirty ...
Small bug-fix release removing the shapely<2 pin in the installation ... Improved handling of GeoDataFrame when the active geometry column is lost from...
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
Hi @arredond is this still something you are planning on working on? This is something I’d quite like to see in geopandas, and if you’re not, I’d like to make a start on it.
For a possible solution, I think we should also look into the suggestion I wrote down in https://github.com/geopandas/geopandas/issues/1153, having a
GeoDataFrame._constructor
that holds this logic of returning a GeoDataFrame or a DataFrame.