Skip to content

Commit

Permalink
fix nan handling in color array
Browse files Browse the repository at this point in the history
  • Loading branch information
knaaptime committed Sep 10, 2024
1 parent 666accc commit dc1ec1a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mapclassify/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def get_color_array(
v = pd.Series(values, dtype=object)
legit_indices = v[~v.isna()].index.values
legit_vals = v.dropna().values
bogus_indices = v[v.isna()].index.values # stash these for use later
# transform (non-NaN) values into class bins
bins = _classify(legit_vals, scheme=scheme, **kwargs).yb

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

# put colors in their correct places and fill empty with designated color
# put colors in their correct places and fill empty with specified color
v.update(colors)
v = v.fillna(f"{nan_color}").apply(np.array)
v.update(nan_colors)

# convert to hexcolors if preferred
if as_hex:
colors = colors.apply(lambda x: to_hex(x / 255.0))
colors = v.apply(lambda x: to_hex(x / 255.0))
return colors.values
return np.stack(v.values)

0 comments on commit dc1ec1a

Please sign in to comment.