Fix HeatMap crash on integer numpy weight arrays#2244
Open
gaoflow wants to merge 1 commit into
Open
Conversation
HeatMap's docstring documents numpy arrays of shape (n, 2) or (n, 3) as valid input, but an all-integer array (dtype int64) crashes at render time with "Object of type int64 is not JSON serializable". validate_location coerces the lat/lon columns to Python float, while the weight column (line[2:]) was passed through unchanged. np.float64 is a subclass of float so json.dumps tolerates float weights, but np.int64 is not a subclass of int, so integer weights fail. This is common in practice, e.g. HeatMap(df[["lat", "lon", "count"]].values) where the count column has an integer dtype. Coerce the weight column to float, mirroring the lat/lon normalization.
Collaborator
|
Very well documented change. Looks good, I will run the automated tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
HeatMap's docstring documents "a numpy.array of shape (n,2) or (n,3)" asvalid input, but an all-integer array crashes at render time:
The float-weight version renders fine. This is common in real usage, e.g.
HeatMap(df[["lat", "lon", "count"]].values)where the weight column has aninteger dtype (pandas
.valuesyields anint64array).Cause
In
HeatMap.__init__,validate_locationcoerces the lat/lon columns to Pythonfloat, but the weight column (line[2:]) is passed through unchanged:np.float64is a subclass offloat, sojson.dumps(via the template's|tojson) tolerates float weights;np.int64is not a subclass ofint,so integer weights raise. The existing
test_heatmap_datamasks this becauseits array contains a
0.5, forcing the whole array tofloat64.Fix
Coerce the weight column to
float, mirroring the lat/lon normalization. TheNaNguard immediately below still works (float(np.nan)is caught bynp.isnan).Tests
test_heatmap_integer_numpy_weights: anint64(n,3) array — weightsnormalized to Python
float, render containsL.heatLayer, and integer vsfloat arrays produce identical
.data.test_heatmap_integer_numpy_no_weight: anint64(n,2) array renders.pytest tests/plugins/passes (the one unrelatedtest_time_slider_choroplethfailure is a pre-existing missing optional dependency, identical on a clean
checkout).
ruff checkclean.