Skip to content

Commit

Permalink
BUG: fix describe_ function when count is not present (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
u3ks committed Jun 17, 2024
1 parent 4719681 commit 3d3faf7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions momepy/functional/_diversity.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def describe_agg(
result.loc[stats.index.values] = stats.values
result.columns = stats.columns
# fill only counts with zeros, other stats are NA
result.loc[:, "count"] = result.loc[:, "count"].fillna(0)
if "count" in result.columns:
result.loc[:, "count"] = result.loc[:, "count"].fillna(0)
result.index.names = result_index.names

return result
Expand Down Expand Up @@ -263,7 +264,8 @@ def describe_reached_agg(
result.loc[stats.index.values] = stats.values
result.columns = stats.columns
# fill only counts with zeros, other stats are NA
result.loc[:, "count"] = result.loc[:, "count"].fillna(0)
if "count" in result.columns:
result.loc[:, "count"] = result.loc[:, "count"].fillna(0)
result.index.name = None

return result
Expand Down
25 changes: 25 additions & 0 deletions momepy/functional/tests/test_diversity.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,18 @@ def test_describe_agg(self):
assert_result(df["sum"], expected_fl_area_sum, self.df_streets)
assert_result(df["mean"], expected_fl_area_mean, self.df_streets)

@pytest.mark.skipif(
not PD_210, reason="aggregation is different in previous pandas versions"
)
def test_describe_cols(self):
df = mm.describe_agg(
self.df_buildings["area"],
self.df_buildings["nID"],
self.df_streets.index,
statistics=["min", "max"],
)
assert list(df.columns) == ["min", "max"]

@pytest.mark.skipif(
not PD_210, reason="aggregation is different in previous pandas versions"
)
Expand Down Expand Up @@ -539,6 +551,19 @@ def test_describe_reached_input_equality(self):
island_result_df.values, island_result_ndarray.values, equal_nan=True
)

@pytest.mark.skipif(
not PD_210, reason="aggregation is different in previous pandas versions"
)
def test_describe_reached_cols(self):
df = mm.describe_reached_agg(
self.df_buildings["fl_area"],
self.df_buildings["nID"],
graph=self.graph_sw,
q=(10, 90),
statistics=["min", "max"],
)
assert list(df.columns) == ["min", "max"]

@pytest.mark.skipif(
not PD_210, reason="aggregation is different in previous pandas versions"
)
Expand Down

0 comments on commit 3d3faf7

Please sign in to comment.