Skip to content

Commit 98d4cee

Browse files
u3ksmartinfleis
andauthored
Fix for Graph.describe() when the graph has a string index (#759) (#760)
* 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]>
1 parent 2f91f3c commit 98d4cee

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

libpysal/graph/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,10 @@ def describe(
26512651
if (y.index != self.unique_ids).all():
26522652
raise ValueError("The values index is not aligned with the graph index.")
26532653

2654+
# reset numerical index to enable numba functionality
2655+
if not isinstance(y.index.dtype, int | float):
2656+
y = y.reset_index(drop=True)
2657+
26542658
if q is None:
26552659
grouper = y.take(self._adjacency.index.codes[1]).groupby(
26562660
self._adjacency.index.codes[0], sort=False

libpysal/graph/tests/test_base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ def test_describe(self):
13271327
# test with isolates and string index
13281328
nybb_contig = graph.Graph.build_contiguity(self.nybb, rook=False)
13291329
stats = nybb_contig.describe(
1330-
self.nybb.geometry.area, statistics=["count", "sum"]
1330+
self.nybb.geometry.area, statistics=["count", "sum", "mode"]
13311331
)
13321332
## all isolate values should be nan
13331333
assert stats.loc["Staten Island"].isna().all()
@@ -1348,6 +1348,13 @@ def test_describe(self):
13481348
check_names=False,
13491349
)
13501350

1351+
y = self.nybb.geometry.area
1352+
for i in nybb_contig.unique_ids:
1353+
neigh_vals = y.loc[nybb_contig[i].index.values]
1354+
expected = neigh_vals.mode().iloc[0] if neigh_vals.shape[0] else 0
1355+
res = stats.loc[i]["mode"]
1356+
assert res == expected
1357+
13511358
## test passing ndarray
13521359
stats1 = nybb_contig.describe(self.nybb.geometry.area, statistics=["sum"])[
13531360
"sum"

0 commit comments

Comments
 (0)