Skip to content

Commit c785f43

Browse files
authored
Merge pull request #1503 from Ade-StapleHill/choropleth_sort
TimeSliderChoropleth sort over daterange '2001-09-09'
2 parents 51f1b7c + 3b3d21a commit c785f43

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

.github/workflows/test_latest_branca.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ jobs:
2525
shell: bash -l {0}
2626
run: |
2727
micromamba activate TEST
28-
micromamba remove branca --yes
28+
micromamba remove branca --yes --force
2929
python -m pip install git+https://github.com/python-visualization/branca.git
3030
python -m pytest -vv --ignore=tests/selenium

folium/plugins/time_slider_choropleth.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ def __init__(self, data, styledict, name=None, overlay=True, control=True,
150150
timestamps = set()
151151
for feature in styledict.values():
152152
timestamps.update(set(feature.keys()))
153-
timestamps = sorted(timestamps)
153+
try:
154+
timestamps = sorted(timestamps, key=int)
155+
except (TypeError, ValueError):
156+
timestamps = sorted(timestamps)
154157

155158
self.timestamps = timestamps
156159
self.styledict = styledict

tests/plugins/test_time_slider_choropleth.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import folium
1212
from folium.plugins import TimeSliderChoropleth
13-
13+
from folium.utilities import normalize
1414

1515
import numpy as np
1616

@@ -19,7 +19,6 @@
1919
import pytest
2020

2121

22-
@pytest.mark.xfail
2322
def test_timedynamic_geo_json():
2423
"""
2524
tests folium.plugins.TimeSliderChoropleth
@@ -29,8 +28,15 @@ def test_timedynamic_geo_json():
2928
datapath = gpd.datasets.get_path('naturalearth_lowres')
3029
gdf = gpd.read_file(datapath)
3130

31+
'''
32+
Timestamps, start date is carefully chosen to be earlier than 2001-09-09
33+
(9 digit timestamp), end date is later (10 digits). This is to ensure an
34+
integer sort is used (and not a string sort were '2' > '10').
35+
datetime.strftime('%s') on Windows just generates date and not timestamp so avoid.
36+
'''
3237
n_periods = 3
33-
dt_index = pd.date_range('2016-1-1', periods=n_periods, freq='M').strftime('%s')
38+
dt_range = pd.Series(pd.date_range('2001-08-1', periods=n_periods, freq='M'))
39+
dt_index = [f"{dt.timestamp():.0f}" for dt in dt_range]
3440

3541
styledata = {}
3642

@@ -71,12 +77,15 @@ def norm(col):
7177
rendered = time_slider_choropleth._template.module.script(time_slider_choropleth)
7278

7379
m._repr_html_()
74-
out = m._parent.render()
80+
out = normalize(m._parent.render())
7581
assert '<script src="https://d3js.org/d3.v4.min.js"></script>' in out
7682

7783
# We verify that data has been inserted correctly
78-
expected_timestamps = """var timestamps = ["1454198400", "1456704000", "1459382400"];""" # noqa
79-
assert expected_timestamps.split(';')[0].strip() == rendered.split(';')[0].strip()
80-
81-
expected_styledict = json.dumps(styledict, sort_keys=True, indent=2)
82-
assert expected_styledict in rendered
84+
expected_timestamps = sorted(dt_index, key=int) # numeric sort
85+
expected_timestamps = "var timestamps = {};".format(expected_timestamps)
86+
expected_timestamps = expected_timestamps.split(';')[0].strip().replace("'", '"')
87+
rendered_timestamps = rendered.split(';')[0].strip()
88+
assert expected_timestamps == rendered_timestamps
89+
90+
expected_styledict = normalize(json.dumps(styledict, sort_keys=True))
91+
assert expected_styledict in normalize(rendered)

0 commit comments

Comments
 (0)