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: Losing the geometry column should convert to a DataFrame

See original GitHub issue

Branched 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:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
m-richardscommented, Aug 7, 2021

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.

0reactions
jorisvandenbosschecommented, Aug 8, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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