Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMPAT: explictly cast numpy floats to native floats #163

Merged
merged 2 commits into from
Apr 3, 2024

Conversation

martinfleis
Copy link
Collaborator

Numpy 2.0 returns np.float64 objects instead of float, so when rendered with jinja2, values may look like [np.float64(3.2),np.float64(4.383333333333334),np.float64(5.566666666666666)] instead of [3.2,4.383333333333334,5.566666666666666]. JavaScript obviously cannot render the former and since there was no error in Python code, the result was just an empty map with errors shows only in the JavaScript console.

I hope these are the only cases where this happens but I have no idea how to test that :D. It fixes python-visualization/folium#1905.

@martinfleis martinfleis requested a review from Conengmo April 3, 2024 15:50
Copy link
Member

@Conengmo Conengmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find! Easy to reproduce now:

m = Map()
LinearColormap(
    ["blue", "yellow", "red", 'green'],
    index=np.array([1, 30, 60, 100]),
    vmin=np.float64(0),
    vmax=100,
).add_to(m)

This fix does fix it.

I was thinking to myself whether it would be better to sanitize on input, but that would require more code in more places. This is more compact. So looks good!

@martinfleis martinfleis merged commit 18ecd5d into python-visualization:main Apr 3, 2024
11 checks passed
@martinfleis martinfleis deleted the numpy2-compat branch April 3, 2024 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Choropleth and branca colormaps do not show up with numpy 2.0
2 participants