Problems when trying to use the add_edge() function

77 views
Skip to first unread message

Dmitriy Tarasov

unread,
Oct 13, 2021, 3:41:10 PM10/13/21
to networkx-discuss

I have a shapefile of points (which I have written to a GeoDataFrame), and for each point, I need to find the nearest node on a graph and add an edge from that node to my point. I was thinking of doing this as follows:

for index, row in subscribers.iterrows(): # Let’s call my points GeoDataFrame “subscribers”
  subscr_location = (row[“Latitude”], row[“Longitude”])
  subscr_node_id = osmnx.get_nearest_node(graph_proj, subscr_location,  method=”euclidean”) # graph_proj is the name of my graph
  subscr_node = nodes_proj.loc[subscr_node_id]
  graph_proj.add_node(subscr_location)
  graph_proj.add_edge(subscr_node, subscr_location)

The last line of this code, however, throws an error, saying that my input is Series, i.e., an unhashable data type. From what I understand, a tuple is the only hashable data type that suits my purposes here, so that’s what I’m trying to pass to add_edge() here—a tuple of two nodes. Why does NetworkX think I’m passing it a Series? Thanks for any advice on this.

Dan Schult

unread,
Oct 13, 2021, 4:01:56 PM10/13/21
to networkx...@googlegroups.com
You are passing the `add_edge` method two inputs. Not a tuple of 2 nodes.
It says that one of the edges is a Series.  I believe that is the `subscr_node` object because you successfully added the `subscr_location` on the previous line. Perhaps you meant to use `subscr_node_id` there?
Or maybe you meant to turn the `loc` into a tuple:    tuple(nodes_prof.loc[subscr_node_id])

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/networkx-discuss/520662a4-df80-408e-8c5e-e44cb9b0930cn%40googlegroups.com.

Dmitriy Tarasov

unread,
Oct 14, 2021, 2:56:30 PM10/14/21
to networkx-discuss
Am I right that the tuple is the only data type in which a pair of coordinates can be passed to add_edge()? If I'm trying to take a point not currently on the graph and connect it to the graph (i.e., turn it into a new node at the end of a new edge), are add_node() and add_edge() the way to do this?

Dan Schult

unread,
Oct 14, 2021, 3:31:35 PM10/14/21
to networkx...@googlegroups.com
>Am I right that the tuple is the only data type in which a pair of coordinates can be passed to add_edge()? If I'm trying to take a point not currently on the graph and connect it to the graph (i.e., turn it into a new node at the end of a new edge), are add_node() and add_edge() the way to do this?
Any data type that is hashable can be passed as a node to `add_edge` and `add_node`. Many containers are not hashable though (like NumPy Arrays and Pandas DataFrames or Series).

To add an edge to a new node to the graph: `G.add_edge` is what you want. You can also use `G.add_node` to include e.g. attributes for the node, but `G.add_edge` adds the node if it is not in the graph already.
Reply all
Reply to author
Forward
0 new messages