Skip to content

Commit 0b55bfc

Browse files
committed
only use gp and opt imports if they are not NoneType
1 parent c1f34f4 commit 0b55bfc

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

optional-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ jupyter
3030
ipykernel
3131

3232
## deps for _county_choropleth.py figure factory
33-
geopandas
3433
pyshp
34+
geopandas
3535
shapely

plotly/figure_factory/_county_choropleth.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
pd.options.mode.chained_assignment = None
1313

14-
gp = optional_imports.get_module('geopandas')
15-
shapefile = optional_imports.get_module('shapefile')
1614
shapely = optional_imports.get_module('shapely')
15+
shapefile = optional_imports.get_module('shapefile')
16+
gp = optional_imports.get_module('geopandas')
17+
1718

1819
def _create_us_counties_df(st_to_state_name_dict, state_to_st_dict):
1920
# URLS
@@ -124,6 +125,7 @@ def _create_us_counties_df(st_to_state_name_dict, state_to_st_dict):
124125
gdf_statefp['ST'] = ST
125126
return gdf_statefp, df_state
126127

128+
127129
st_to_state_name_dict = {
128130
'AK': 'Alaska',
129131
'AL': 'Alabama',
@@ -237,16 +239,6 @@ def _create_us_counties_df(st_to_state_name_dict, state_to_st_dict):
237239
'Wyoming': 'WY'
238240
}
239241

240-
df, df_state = _create_us_counties_df(st_to_state_name_dict, state_to_st_dict)
241-
242-
243-
fips_polygon_map = dict(
244-
zip(
245-
df['FIPS'].tolist(),
246-
df['geometry'].tolist()
247-
)
248-
)
249-
250242
USA_XRANGE = [-125.0, -65.0]
251243
USA_YRANGE = [25.0, 49.0]
252244

@@ -303,7 +295,7 @@ def _intervals_as_labels(array_of_intervals, round_legend_values, exponent_forma
303295

304296
def _calculations(df, fips, values, index, f, simplify_county, level,
305297
x_centroids, y_centroids, centroid_text, x_traces,
306-
y_traces):
298+
y_traces, fips_polygon_map):
307299
if fips_polygon_map[f].type == 'Polygon':
308300
x = fips_polygon_map[f].simplify(
309301
simplify_county
@@ -336,9 +328,11 @@ def _calculations(df, fips, values, index, f, simplify_county, level,
336328
x_c = [poly.centroid.xy[0].tolist() for poly in fips_polygon_map[f]]
337329
y_c = [poly.centroid.xy[1].tolist() for poly in fips_polygon_map[f]]
338330

331+
county_name_str = str(df[df['FIPS'] == f]['COUNTY_NAME'].iloc[0])
332+
state_name_str = str(df[df['FIPS'] == f]['STATE_NAME'].iloc[0])
339333
text = (
340-
'County: ' + df[df['FIPS'] == f]['COUNTY_NAME'].iloc[0] + '<br>' +
341-
'State: ' + df[df['FIPS'] == f]['STATE_NAME'].iloc[0] + '<br>' +
334+
'County: ' + county_name_str + '<br>' +
335+
'State: ' + state_name_str + '<br>' +
342336
'FIPS: ' + str(f) + '<br>Value: ' + str(values[index])
343337
)
344338
t_c = [text for poly in fips_polygon_map[f]]
@@ -358,8 +352,8 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
358352
show_hover=True, show_state_data=True,
359353
state_outline=None, county_outline=None,
360354
centroid_marker=None, round_legend_values=False,
361-
exponent_format=False, legend_title='', df=df,
362-
df_state=df_state, **layout_options):
355+
exponent_format=False, legend_title='',
356+
**layout_options):
363357
"""
364358
Returns figure for county choropleth. Uses data from package_data.
365359
@@ -566,6 +560,16 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
566560
raise ImportError("geopandas, shapefile and shapely must be "
567561
"installed for this figure factory")
568562

563+
df, df_state = _create_us_counties_df(st_to_state_name_dict,
564+
state_to_st_dict)
565+
566+
fips_polygon_map = dict(
567+
zip(
568+
df['FIPS'].tolist(),
569+
df['geometry'].tolist()
570+
)
571+
)
572+
569573
if not state_outline:
570574
state_outline = {'color': 'rgb(240, 240, 240)',
571575
'width': 1}
@@ -707,7 +711,7 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
707711
y_centroids, centroid_text) = _calculations(
708712
df, fips, values, index, f, simplify_county, level,
709713
x_centroids, y_centroids, centroid_text, x_traces,
710-
y_traces
714+
y_traces, fips_polygon_map
711715
)
712716
except KeyError:
713717
fips_not_in_shapefile.append(f)
@@ -726,7 +730,7 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
726730
y_centroids, centroid_text) = _calculations(
727731
df, fips, values, index, f, simplify_county, level,
728732
x_centroids, y_centroids, centroid_text, x_traces,
729-
y_traces
733+
y_traces, fips_polygon_map
730734
)
731735
except KeyError:
732736
fips_not_in_shapefile.append(f)

0 commit comments

Comments
 (0)