holoviz / spatialpandas

Pandas extension arrays for spatial/geometric operations
BSD 2-Clause "Simplified" License
308 stars 25 forks source link

Being able to instanciate empty GeoDataFrame #121

Open rderollepot opened 1 year ago

rderollepot commented 1 year ago

Is your feature request related to a problem? Please describe.

I have a workflow in which I process a geopandas GeoDataFrame in order to draw a map on a dashboard. At some point, I wish to make use of datashader reduction capabilities and thus, I convert my df into a spatialpandas GeoDataFrame. My dashboard allows to set filters which can lead my GeoDataFrame to be empty, and in that case, the conversion into a spatialpandas GeoDataFrame fails:

ValueError: A spatialpandas GeoDataFrame must contain at least one spatialpandas GeometryArray column

This led me to realize that spatialpandas does not allow the instanciation of an empty DataFrame, or at least not in the way pandas and geopandas allow it:

import pandas as pd
import geopandas as gpd
import spatialpandas as spd

pd.DataFrame() # works
gpd.GeoDataFrame() # works
spd.GeoDataFrame() # fails

Describe the solution you'd like

I would like to be able to generate empty spatialpandas GeoDataFrame.

Describe alternatives you've considered

An alternative I have is to first convert into a spatialpandas GeoDataFrame, and then filter this new DataFrame. When I do it in this order, query() is able to return an empty spatialpandas GeoDataFrame if there are indeed no matching results.

ianthomas23 commented 1 year ago

This should be fairly easy to implement, the question is whether it will break anything because of the previously valid assumption that a GeoDataFrame always contains data. I'll take a look.