Skip to content

Commit

Permalink
Fix for Graph.describe() when the graph has a string index (#759) (#760)
Browse files Browse the repository at this point in the history
* numba mode fix when graph has string index

* typing

* Apply suggestions from code review

Co-authored-by: Martin Fleischmann <[email protected]>

---------

Co-authored-by: Martin Fleischmann <[email protected]>
  • Loading branch information
u3ks and martinfleis authored Aug 5, 2024
1 parent 2f91f3c commit 98d4cee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,10 @@ def describe(
if (y.index != self.unique_ids).all():
raise ValueError("The values index is not aligned with the graph index.")

# reset numerical index to enable numba functionality
if not isinstance(y.index.dtype, int | float):
y = y.reset_index(drop=True)

if q is None:
grouper = y.take(self._adjacency.index.codes[1]).groupby(
self._adjacency.index.codes[0], sort=False
Expand Down
9 changes: 8 additions & 1 deletion libpysal/graph/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ def test_describe(self):
# test with isolates and string index
nybb_contig = graph.Graph.build_contiguity(self.nybb, rook=False)
stats = nybb_contig.describe(
self.nybb.geometry.area, statistics=["count", "sum"]
self.nybb.geometry.area, statistics=["count", "sum", "mode"]
)
## all isolate values should be nan
assert stats.loc["Staten Island"].isna().all()
Expand All @@ -1348,6 +1348,13 @@ def test_describe(self):
check_names=False,
)

y = self.nybb.geometry.area
for i in nybb_contig.unique_ids:
neigh_vals = y.loc[nybb_contig[i].index.values]
expected = neigh_vals.mode().iloc[0] if neigh_vals.shape[0] else 0
res = stats.loc[i]["mode"]
assert res == expected

## test passing ndarray
stats1 = nybb_contig.describe(self.nybb.geometry.area, statistics=["sum"])[
"sum"
Expand Down

0 comments on commit 98d4cee

Please sign in to comment.