Skip to content

Commit e8fd4c0

Browse files
committed
finaliz roundtrippable from_adjlist()
1 parent 708eae8 commit e8fd4c0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

libpysal/weights/weights.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ def from_adjlist(
262262
if try_weightcol is None:
263263
adjlist = adjlist.copy(deep=True)
264264
adjlist["weight"] = 1
265-
all_ids = set(adjlist[focal_col].tolist())
266-
all_ids |= set(adjlist[neighbor_col].tolist())
267265
grouper = adjlist.groupby(focal_col)
268-
neighbors = grouper[neighbor_col].apply(list).to_dict()
269-
weights = grouper[weight_col].apply(list).to_dict()
270-
neighbors.update({k: [] for k in all_ids.difference(list(neighbors.keys()))})
271-
weights.update({k: [] for k in all_ids.difference(list(weights.keys()))})
266+
neighbors = dict()
267+
weights = dict()
268+
for ix, chunk in grouper:
269+
neighbors_to_ix = chunk[neighbor_col].values
270+
weights_to_ix = chunk[weight_col].values
271+
mask = neighbors_to_ix != ix
272+
neighbors[ix] = neighbors_to_ix[mask].tolist()
273+
weights[ix] = weights_to_ix[mask].tolist()
272274
return cls(neighbors=neighbors, weights=weights)
273275

274276
def to_adjlist(

0 commit comments

Comments
 (0)