Skip to content

Commit dc1ec1a

Browse files
author
knaaptime
committed
fix nan handling in color array
1 parent 666accc commit dc1ec1a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mapclassify/util.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def get_color_array(
5959
v = pd.Series(values, dtype=object)
6060
legit_indices = v[~v.isna()].index.values
6161
legit_vals = v.dropna().values
62+
bogus_indices = v[v.isna()].index.values # stash these for use later
6263
# transform (non-NaN) values into class bins
6364
bins = _classify(legit_vals, scheme=scheme, **kwargs).yb
6465

@@ -69,13 +70,16 @@ def get_color_array(
6970
# generate RBGA array and convert to series
7071
rgbas = colormaps[cmap](normalized_vals, bytes=True, alpha=alpha)
7172
colors = pd.Series(list(rgbas), index=legit_indices).apply(np.array)
73+
nan_colors = pd.Series(
74+
[nan_color for i in range(len(bogus_indices))], index=bogus_indices
75+
).apply(lambda x: np.array(x).astype(np.uint8))
7276

73-
# put colors in their correct places and fill empty with designated color
77+
# put colors in their correct places and fill empty with specified color
7478
v.update(colors)
75-
v = v.fillna(f"{nan_color}").apply(np.array)
79+
v.update(nan_colors)
7680

7781
# convert to hexcolors if preferred
7882
if as_hex:
79-
colors = colors.apply(lambda x: to_hex(x / 255.0))
83+
colors = v.apply(lambda x: to_hex(x / 255.0))
8084
return colors.values
8185
return np.stack(v.values)

0 commit comments

Comments
 (0)