Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: fix describe_ function when count is not present #608

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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