Skip to content

Choropleth doesn't bind if using numeric index #1185

Closed
@alessioarena

Description

@alessioarena

Please add a code sample or a nbviewer link, copy-pastable if possible

gdf = gpd.read_file('SA2 shapefile')
gdf.columns
# output: ['name', 'probability']
gdf.index.dtype
# output: dtype('int64')

m = folium.Map(location=[	-37, 144], zoom_start=7, tiles='cartodbpositron')
folium.Choropleth(
    geo_data=gdf.geometry,
    data= gdf.probability,
    key_on='feature.id',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    nan_fill_color= 'purple',
    legend_name='Probability',
    highlight=True
).add_to(m)

Problem description

I'm pretty new to folium, but I did work with geopandas and pandas a lot.
I also cannot provide the data, but that should be reproducible with any shapefile if the problem is what I think is.

My understanding of the code above is that will try to:

  • select the geometry column and pass it as a GeoSeries for conversion to GeoJSON
  • select the probability column and pass it as a Series
  • leveraging the common index, trying to bind the two on feature.id <-> index
  • do some magic and spit the choropleth out

Unfortunately this is not working as expected, as no error was raised but all polygons have a nice purple color (NaN). The legend was correctly generated so I reckon is purely a merging/binding issue.

Doing some quick troubleshooting I suspect that the bind failed because the feature.id is now a string, while the Series index is numeric, but I may be very wrong.

This instead worked as expected:

folium.Choropleth(
    geo_data=gdf,
    data= gdf,
    columns=['name', 'probability'],
    key_on='feature.properties.name',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    nan_fill_color= 'purple',
    legend_name='Probability',
    highlight=True
).add_to(m)

Happy to do additional troubleshooting if needed.

Expected Output

A nice colorful choropleth without NaNs (they were dropped at a previous step)

Output of folium.__version__

'0.9.1'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions