Skip to content

Commit 82c3d5a

Browse files
Ade-StapleHillocefpaf
authored andcommitted
plugin.TimeSliderChoropleth numeric timestamp sort (#1502)
For description of problem see: [plugins.TimeSliderChoropleth() time slider bar out of order for date range that spans '2001-09-09' #1502](#1502) Files changed: folium/plugins/time_slider_choropleth.py: - try numeric sort first - if not numeric e.g. date strings, and exception thrown, then fall back to generic sort. - Problem if string as alphabetic sort ('2' > '10') may result in out of order tests/plugins/test_time_slider_choropleth.py: - No longer expected fail - Avoid hardwiring in timestamp values - Avoid the convenient call to datetime.strftime('%s') for timestamps as Windows only returns string date - Carefully chosen date range to span 2001-09-09 Ran tests and qa: - flake8 --max-line-length=120 tests/plugins/test_time_slider_choropleth.py folium/plugins/time_slider_choropleth.py - python -m pytest tests/plugins/test_time_slider_choropleth.py (previous commit fixed xfail)
1 parent 51f1b7c commit 82c3d5a

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

folium/plugins/time_slider_choropleth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ 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+
timestamps = list(timestamps)
154+
try:
155+
timestamps = sorted(timestamps, key=int)
156+
except (TypeError, ValueError):
157+
timestamps = sorted(timestamps)
154158

155159
self.timestamps = timestamps
156160
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)