From 62ef2f5b2823a77d33c53a0658f115c862a37a49 Mon Sep 17 00:00:00 2001 From: Volodymyr Borysenko Date: Sat, 28 Nov 2026 01:14:13 -0800 Subject: [PATCH] Render numpy and pandas input in HeatMapWithTime as JSON --- folium/plugins/heat_map_withtime.py | 19 +++++++++++----- tests/plugins/test_heat_map_withtime.py | 30 +++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/folium/plugins/heat_map_withtime.py b/folium/plugins/heat_map_withtime.py index 9f62fb4b31..634b66bae5 100644 --- a/folium/plugins/heat_map_withtime.py +++ b/folium/plugins/heat_map_withtime.py @@ -1,7 +1,7 @@ from folium.elements import JSCSSMixin from folium.map import Layer from folium.template import Template -from folium.utilities import none_max, none_min +from folium.utilities import if_pandas_df_convert_to_numpy, none_max, none_min class HeatMapWithTime(JSCSSMixin, Layer): @@ -147,7 +147,7 @@ class HeatMapWithTime(JSCSSMixin, Layer): {times : times, currentTime: new Date(1)} ); - var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, { + var {{this._control_name}} = new L.Control.TimeDimensionCustom({{ this.index|tojson }}, { autoPlay: {{this.auto_play}}, backwardButton: {{this.backward_button}}, displayDate: {{this.display_index}}, @@ -169,7 +169,7 @@ class HeatMapWithTime(JSCSSMixin, Layer): }) .addTo({{this._parent.get_name()}}); - var {{this.get_name()}} = new TDHeatmap({{this.data}}, + var {{this.get_name()}} = new TDHeatmap({{ this.data|tojson }}, {heatmapOptions: { radius: {{this.radius}}, blur: {{this.blur}}, @@ -238,9 +238,18 @@ def __init__( self._control_name = self.get_name() + "Control" # Input data. - self.data = data + # Plain floats and strings, so that numpy and pandas input renders as JSON. + self.data = [ + [ + [float(value) for value in point] + for point in if_pandas_df_convert_to_numpy(frame) + ] + for frame in data + ] self.index = ( - index if index is not None else [str(i) for i in range(1, len(data) + 1)] + [str(label) for label in index] + if index is not None + else [str(i) for i in range(1, len(data) + 1)] ) if len(self.data) != len(self.index): raise ValueError( diff --git a/tests/plugins/test_heat_map_withtime.py b/tests/plugins/test_heat_map_withtime.py index 320cfc77d7..dd70750a9c 100644 --- a/tests/plugins/test_heat_map_withtime.py +++ b/tests/plugins/test_heat_map_withtime.py @@ -4,6 +4,7 @@ """ import numpy as np +import pytest import folium from folium import plugins @@ -41,7 +42,7 @@ def test_heat_map_with_time(): {times : times, currentTime: new Date(1)} ); - var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, { + var {{this._control_name}} = new L.Control.TimeDimensionCustom({{ this.index|tojson }}, { autoPlay: {{this.auto_play}}, backwardButton: {{this.backward_button}}, displayDate: {{this.display_index}}, @@ -63,7 +64,7 @@ def test_heat_map_with_time(): }) .addTo({{this._parent.get_name()}}); - var {{this.get_name()}} = new TDHeatmap({{this.data}}, + var {{this.get_name()}} = new TDHeatmap({{ this.data|tojson }}, {heatmapOptions: { radius: {{this.radius}}, blur: {{this.blur}}, @@ -93,3 +94,28 @@ def test_heat_map_with_time_bounds(): assert hm.get_bounds() == [[48.0, 3.0], [52.0, 6.0]] assert m.get_bounds() == [[48.0, 3.0], [52.0, 7.0]] + + +def test_heat_map_with_time_numpy_and_pandas_input(): + pd = pytest.importorskip("pandas") + data = np.array([[[48.0, 5.0], [49.0, 6.0]], [[48.5, 5.5], [49.5, 6.5]]]) + index = pd.to_datetime(["2020-01-01", "2020-01-02"]) + m = folium.Map() + hm = plugins.HeatMapWithTime(data, index=index).add_to(m) + + out = normalize(m._parent.render()) + + expected_data = "[[[48.0,5.0],[49.0,6.0]],[[48.5,5.5],[49.5,6.5]]]" + assert f"new TDHeatmap({expected_data}," in out + expected_index = '["2020-01-01 00:00:00","2020-01-02 00:00:00"]' + assert f"new L.Control.TimeDimensionCustom({expected_index}," in out + assert hm.get_bounds() == [[48.0, 5.0], [49.5, 6.5]] + + frames = [ + pd.DataFrame({"lat": [48.0, 49.0], "lng": [5.0, 6.0], "w": [0.5, 1.0]}), + pd.DataFrame({"lat": [50.0], "lng": [7.0], "w": [0.2]}), + ] + hm = plugins.HeatMapWithTime(frames).add_to(m) + out = normalize(m._parent.render()) + expected_data = "[[[48.0,5.0,0.5],[49.0,6.0,1.0]],[[50.0,7.0,0.2]]]" + assert f"new TDHeatmap({expected_data}," in out