In current version of geopandas, I couldn't figure out a way to store a GeoSeries as a non-geometry column. For example:
import geopandas as gpd
from shapely.geometry import Point
gdf = gpd.GeoDataFrame(geometry=gpd.Geoseries())
gdf['AnotherPoint'] = gpd.GeoSeries([Point(0, 1)])
print(type(gdf['AnotherPoint']))
<class 'pandas.core.series.Series'>
Is there any way to get around this? I totally understand that there can only be one column used as geometric index. But being able to switch between them will be great, if filtering data with multiple geometric feature is needed.
I use multiple geometric columns often. You can switch between which one is considered the "active" geometric column using the set_geometry method, if the two series are geoseries. So, for instance, on the NY borough example data:
import geopandas as gpd
example = gpd.read_file(gpd.datasets.get_path('nybb'))
you can make a new geoseries:
example['buffers'] = example.buffer(5000)
and swap between them using set_geometry, even in chained methods:
example.set_geometry('buffers').plot() # plots the buffers only
I should also note, the series could be a pandas.core.series.Series object and, if Geopandas can interpret its contents as geometries through shapely, it'll get converted to a GeoSeries at the point of set_geometry.
@ljwolf
Thanks for your response! However, I still have several confusions:
What is the cost of converting a Series of shapely object to GeoSeries?
Is there any way to avoid losing CRS information when switching geometric column?
Is there a way to apply conditions on multiple geometric columns? (If a GeoSeries can exist as a non-geometric column, then I can just perform conditions on all of these columns and use & to connect them).
@ljwolf
I would like to thank you again! I have solved my problem in a slightly different approach.
Most helpful comment
I use multiple geometric columns often. You can switch between which one is considered the "active" geometric column using the
set_geometrymethod, if the two series are geoseries. So, for instance, on the NY borough example data:you can make a new geoseries:
and swap between them using
set_geometry, even in chained methods: