ramonhagenaars / nptyping

💡 Type hints for Numpy and Pandas
MIT License
593 stars 30 forks source link

Implement a geodataframe class #116

Open cojmeister opened 8 months ago

cojmeister commented 8 months ago

Implement a geodataframe typing that can be use to check geodataframes - as of now my static tests with beartype fail when using this. Beartype is right to fail this.

Example of failing code.

import geopandas as gpd
import shapely
from nptyping import DataFrame, Structure as S
from beartype import beartype

gdf = gpd.GeoDataFrame(
    {"center_x": [0, 1], "center_y": [0, 1]}
)
gdf['geometry'] = gdf.apply(lambda row: shapely.Point(row["center_x"], row["center_y"]).buffer(10), axis=1)

gdf.set_geometry("geometry", inplace=True)

@beartype
def some_function(some_geodataframe: DataFrame[S["center_x: Float, center_y: Float, geometry: Polygon"]]) -> None:
    print(some_geodataframe)

some_function(gdf)

Proposed solution:

Create a geodataframe class that just inherits from dataframe and has a geometry column that will be a shapely geometry (a bit to general). Another option is to allow for classes that inherit from dataframe.

I am willing to write the code and do the changes 😊