Skip to content

Commit 8ca54f4

Browse files
committed
actually handle islands in adjlist builder
1 parent d47b376 commit 8ca54f4

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

libpysal/weights/weights.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,15 @@ def to_adjlist(
311311
"{} islands in this weights matrix. Conversion to an "
312312
"adjacency list will drop these observations!".format(len(self.islands))
313313
)
314-
adjlist = pd.DataFrame(
315-
((idx, n, w) for idx, neighb in self for n, w in list(neighb.items())),
316-
columns=("focal", "neighbor", "weight"),
317-
)
314+
links = []
315+
for idx, neighb in self:
316+
if len(neighb) == 0:
317+
links.append((idx, None, numpy.nan))
318+
continue
319+
for n, w in neighb.items():
320+
links.append((idx, n, w))
321+
adjlist = pd.DataFrame(links, columns=[focal_col, neighbor_col, weight_col])
322+
318323
return adjtools.filter_adjlist(adjlist) if remove_symmetric else adjlist
319324

320325
def to_networkx(self):

0 commit comments

Comments
 (0)