Skip to content

Commit 74243fb

Browse files
LeandroOrdonezConengmo
authored andcommitted
Explicit key_of_x check in Choropleth (#1169)
Add explicit check for None in color_scale_fun(x) from class Choropleth. Add test.
1 parent 40b2860 commit 74243fb

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

folium/features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ def get_by_key(obj, key):
11091109

11101110
def color_scale_fun(x):
11111111
key_of_x = get_by_key(x, key_on)
1112-
if not key_of_x:
1112+
if key_of_x is None:
11131113
raise ValueError("key_on `{!r}` not found in GeoJSON.".format(key_on))
11141114

11151115
if key_of_x not in color_data.keys():

tests/geo_grid.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{"type": "FeatureCollection", "features": [
2+
{"id": "0", "type": "Feature", "properties": {"idx": 0}, "geometry": {"type": "Polygon", "coordinates": [[[479978.6866107661, 6676603.690234357], [491978.6866107661, 6676603.690234357], [491978.6866107661, 6664603.690234357], [479978.6866107661, 6664603.690234357], [479978.6866107661, 6676603.690234357]]]}},
3+
{"id": "1", "type": "Feature", "properties": {"idx": 1}, "geometry": {"type": "Polygon", "coordinates": [[[479978.6866107661, 6664603.690234357], [491978.6866107661, 6664603.690234357], [491978.6866107661, 6652603.690234357], [479978.6866107661, 6652603.690234357], [479978.6866107661, 6664603.690234357]]]}},
4+
{"id": "2", "type": "Feature", "properties": {"idx": 2}, "geometry": {"type": "Polygon", "coordinates": [[[479978.6866107661, 6652603.690234357], [491978.6866107661, 6652603.690234357], [491978.6866107661, 6640603.690234357], [479978.6866107661, 6640603.690234357], [479978.6866107661, 6652603.690234357]]]}},
5+
{"id": "3", "type": "Feature", "properties": {"idx": 3}, "geometry": {"type": "Polygon", "coordinates": [[[491978.6866107661, 6676603.690234357], [503978.6866107661, 6676603.690234357], [503978.6866107661, 6664603.690234357], [491978.6866107661, 6664603.690234357], [491978.6866107661, 6676603.690234357]]]}},
6+
{"id": "4", "type": "Feature", "properties": {"idx": 4}, "geometry": {"type": "Polygon", "coordinates": [[[491978.6866107661, 6664603.690234357], [503978.6866107661, 6664603.690234357], [503978.6866107661, 6652603.690234357], [491978.6866107661, 6652603.690234357], [491978.6866107661, 6664603.690234357]]]}},
7+
{"id": "5", "type": "Feature", "properties": {"idx": 5}, "geometry": {"type": "Polygon", "coordinates": [[[491978.6866107661, 6652603.690234357], [503978.6866107661, 6652603.690234357], [503978.6866107661, 6640603.690234357], [491978.6866107661, 6640603.690234357], [491978.6866107661, 6652603.690234357]]]}}
8+
]}

tests/test_folium.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ def test_choropleth_features(self):
230230
assert '"fillColor":"a_random_color","fillOpacity":0.123454321' in out_str
231231
assert '"fillOpacity":0.543212345' in out_str
232232

233+
def test_choropleth_key_on(self):
234+
"""Test to make sure that Choropleth function doesn't raises
235+
a ValueError when the 'key_on' field is set to a column that might
236+
have 0 as a value.
237+
"""
238+
with open(os.path.join(rootpath, 'geo_grid.json')) as f:
239+
geo_data = json.load(f)
240+
data = pd.DataFrame({'idx': {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5},
241+
'value': {'0': 78.0, '1': 39.0, '2': 0.0, '3': 81.0, '4': 42.0, '5': 68.0}})
242+
fill_color = 'BuPu'
243+
columns = ['idx', 'value']
244+
key_on = 'feature.properties.idx'
245+
246+
Choropleth(
247+
geo_data=geo_data,
248+
data=data,
249+
key_on=key_on,
250+
fill_color=fill_color,
251+
columns=columns)
252+
233253
def test_choropleth_warning(self):
234254
"""Test that the Map.choropleth method works and raises a warning."""
235255
self.setup()

0 commit comments

Comments
 (0)