diff --git a/momepy/diversity.py b/momepy/diversity.py index 78c924fd..048ff71f 100644 --- a/momepy/diversity.py +++ b/momepy/diversity.py @@ -728,6 +728,8 @@ class Unique: spatial weights matrix unique_id : str name of the column with unique id used as ``spatial_weights`` index + dropna : bool (default True) + Don’t include NaN in the counts of unique values. verbose : bool (default True) if True, shows progress bars in loops and indication of steps @@ -754,7 +756,9 @@ class Unique: 100%|██████████| 144/144 [00:00<00:00, 722.50it/s] """ - def __init__(self, gdf, values, spatial_weights, unique_id, verbose=True): + def __init__( + self, gdf, values, spatial_weights, unique_id, dropna=True, verbose=True + ): self.gdf = gdf self.sw = spatial_weights self.id = gdf[unique_id] @@ -775,7 +779,7 @@ def __init__(self, gdf, values, spatial_weights, unique_id, verbose=True): neighbours += spatial_weights.neighbors[index] values_list = data.loc[neighbours] - results_list.append(len(values_list.unique())) + results_list.append(values_list.nunique(dropna=dropna)) else: results_list.append(np.nan) diff --git a/tests/test_diversity.py b/tests/test_diversity.py index 93953884..a2966d68 100644 --- a/tests/test_diversity.py +++ b/tests/test_diversity.py @@ -169,6 +169,13 @@ def test_Unique(self): assert un.isna().any() assert un[5] == 8 + self.df_tessellation.loc[0, "cat"] = np.nan + un = mm.Unique(self.df_tessellation, "cat", self.sw, "uID", dropna=False).series + assert un[0] == 9 + + un = mm.Unique(self.df_tessellation, "cat", self.sw, "uID", dropna=True).series + assert un[0] == 8 + def test_Percentile(self): perc = mm.Percentiles(self.df_tessellation, "area", self.sw, "uID").frame assert np.all(