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.

Creation of geodataframe shouldn't modify the original dataframe

See original GitHub issue

I’m using geopandas 0.5.1 for the example. Creating a geodataframe from a Pandas dataframe modifies the input dataframe, even when the geometry is not a column of the input dataframe. This creates an issue especially when the same dataframe is used to create multiple geodataframes, in which case the first geometry definition would always prevail in subsequent operations.

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
df = pd.DataFrame({'Name':['Adam','Barbara','Cat','Doug','Ezra','Fiona','Greg'],
                  'lat':[35.5,35.2,34.89,36.1,35.78,35.76,34.99],
                  'lon':[-112.02,-111.09,-115.3,-114.01,-113.02,-112.9,-112.02]})
gdf = gpd.GeoDataFrame(df, geometry = [Point(x,y) for x, y in zip(df.lon,df.lat)])
print(gdf.columns)
>>> Index(['Name', 'lat', 'lon', 'geometry'], dtype='object')
print(df.columns)
>>> Index(['Name', 'lat', 'lon', 'geometry'], dtype='object')

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mvonpohlecommented, May 20, 2022

I’m still running into this issue using python 3.10.4, GeoPandas 0.10.2, and Pandas 1.4.2.

1reaction
Sangarshanancommented, Mar 12, 2020

Sure…will open a PR for this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error while converting Dataframe to Geodataframe
I just tested your code* with pandas=0.24.1 and geopandas=0.4.0, and it ran without error. For what it's worth, the geopandas documentation ...
Read more >
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 >
Pandas: Knowing when an operation affects the original ...
So now, I just certify that original is not changing and use this peace of code to the program at begining to remove...
Read more >
03_yfcc_gridagg_hll
In addition to the steps listed in the raw notebook, this notebooks describes, ... GeoDataFrame: """Convert grid pandas DataFrame to geopandas ...
Read more >
SettingwithCopyWarning: How to Fix This Warning in Pandas
Depending on what we're doing we might want to be modifying the original df1 (left), or we might want to be modifying only...
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