diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index fe4ee038b..6c13bda48 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -189,6 +189,45 @@ def to_W(self): # noqa: N802 ------- libpysal.weights.W representation of graph as a weights.W object + + Examples + -------- + >>> import geopandas as gpd + >>> from geodatasets import get_path + >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") + >>> nybb + BoroCode ... geometry + BoroName ... + Staten Island 5 ... MULTIPOLYGON (((970217.022 145643.332, 970227.... + Queens 4 ... MULTIPOLYGON (((1029606.077 156073.814, 102957... + Brooklyn 3 ... MULTIPOLYGON (((1021176.479 151374.797, 102100... + Manhattan 1 ... MULTIPOLYGON (((981219.056 188655.316, 980940.... + Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278... + [5 rows x 4 columns] + + >>> contiguity = graph.Graph.build_contiguity(nybb) + >>> contiguity.adjacency + focal neighbor + Staten Island Staten Island 0 + Queens Brooklyn 1 + Manhattan 1 + Bronx 1 + Brooklyn Queens 1 + Manhattan 1 + Manhattan Queens 1 + Brooklyn 1 + Bronx 1 + Bronx Queens 1 + Manhattan 1 + Name: weight, dtype: int64 + + >>> w = contiguity.to_W() + >>> w.neighbors + {'Bronx': ['Queens', 'Manhattan'], + 'Brooklyn': ['Queens', 'Manhattan'], + 'Manhattan': ['Queens', 'Brooklyn', 'Bronx'], + 'Queens': ['Brooklyn', 'Manhattan', 'Bronx'], + 'Staten Island': []} """ grouper = self._adjacency.groupby(level=0) neighbors = {}