pysal.lib.weights.remap_ids

pysal.lib.weights.remap_ids(w, old2new, id_order=[])[source]

Remaps the IDs in a spatial weights object.

Parameters:
w : W

Spatial weights object

old2new : dictionary

Dictionary where the keys are the IDs in w (i.e. “old IDs”) and the values are the IDs to replace them (i.e. “new IDs”)

id_order : list

An ordered list of new IDs, which defines the order of observations when iterating over W. If not set then the id_order in w will be used.

Returns:
implicit : W

Spatial weights object with new IDs

Examples

>>> from pysal.lib.weights import lat2W
>>> w = lat2W(3,2)
>>> w.id_order
[0, 1, 2, 3, 4, 5]
>>> w.neighbors[0]
[2, 1]
>>> old_to_new = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
>>> w_new = remap_ids(w, old_to_new)
>>> w_new.id_order
['a', 'b', 'c', 'd', 'e', 'f']
>>> w_new.neighbors['a']
['c', 'b']