Creation of geodataframe shouldn't modify the original dataframe
See original GitHub issueI’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:
- Created 4 years ago
- Reactions:6
- Comments:8 (5 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
I’m still running into this issue using python 3.10.4, GeoPandas 0.10.2, and Pandas 1.4.2.
Sure…will open a PR for this