How to convert a GeoSeries to a GeoDataFrame with Geopandas

GeoDataframe now accepts a geometry keyword argument. Taking advantage of that, we can write

envgdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(env))

This automatically sets the GeoSeries as the geometry column.


The type of env here is a Shapely Polygon. In this line

envgdf['geometry'] = env

You're trying to assign a Polygon to a Geometry column. You can instead create a Geoseries from the Polygon and create a Geodataframe based on that. Here's the updated code:

import sys
import geopandas as gpd

shp = (sys.argv[1])

gdf = gpd.read_file(shp)

union = gdf.unary_union

env = union.envelope

# Replace these three lines:

#envgdf = gpd.GeoDataFrame()
#envgdf['geometry'] = env
#envgdf.geometry.name

# With
envgdf = gpd.GeoDataFrame(gpd.GeoSeries(env))

# Edit: Following Tim C's comment below, this line sets geometry for resulting geodataframe
envgdf = envgdf.rename(columns={0:'geometry'}).set_geometry('geometry')

print("\nGeoDataFrame :\n", envgdf)