Index out of range with geoplot

35 Views Asked by At

I am trying to create a KDE plot to show the geographic density of each crime category. The dataset is the Philadelphia Crime Incidents and the Philadelphia Police District Boundaries.

import geopandas as gpd
import geoplot as gplt

gdf = data.copy()
gdf['Coordinates'] = list(zip(gdf.point_x, gdf.point_y))
gdf.Coordinates = gdf.Coordinates.apply(Point)
gdf = gpd.GeoDataFrame(
    gdf,
    geometry='Coordinates',
    crs="epsg:4326"
)

# GDF of crime incidents
crime_data = gdf.loc[gdf["text_general_code"] == 'Thefts']

# Boundaries of the region
land = pd_boundary.unary_union
land = gpd.GeoDataFrame(gpd.GeoSeries(land), columns=["geometry"])
land = land.set_geometry("geometry")
land.crs = "epsg:4326"

When I try to draw a KDE plot, I am unusually getting IndexError: index out of range. I have tried checking for null values and also searched online for probable cause but didn't help.

ax = gplt.polyplot(land)
gplt.kdeplot(
    crime_data,
    ax=ax
)

Any ideas on how to debug would be very helpful.

1

There are 1 best solutions below

1
jhmt On

I test your code on an example data and was able to reproduce your issue. If I removed all na values from data I was able to make the figure. So you can insert these lines of code before making the gpd.GeoDataFrame:

data_na = data.dropna(axis=0)
gdf = data_na.copy()