7
import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
#--
edgelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew    /e570c38bcc72a8d102422f2af836513b/raw/89c76b2563dbc0e88384719a35cba0dfc04cd522/edgelist_sleeping_giant.csv')
edgelist.head(10)
#--
nodelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv')
nodelist.head(5)
#--
g = nx.Graph()
#--
for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
#--
#print(elrow[0])
#print(elrow[1])
#print(elrow[2:].to_dict())
#--
g.edges(data=True)[0:5]
g.nodes(data=True)[0:10]
#--
print(format(g.number_of_edges()))
print(format(g.number_of_nodes()))

Gets me the following error:

Traceback (most recent call last):
  File "C:/Users/####/Main.py", line 22, in <module>
    g.edges(data=True)[0:5]
TypeError: 'EdgeDataView' object is not subscriptable

I have read a couple of other threads but nada. From my simple understanding the error is caused by [0:5] but im most likely wrong.

I'm a fairly basic coder and am trying to follow this tutorial and i get the error above.

rodgdor
  • 2,530
  • 1
  • 19
  • 26
Aarron Cooke
  • 83
  • 1
  • 3
  • What is the type of `g.edges` here? it's probable that this is not subscriptable as it's a dict or something, post the output from `print(type(g.edges(data=True)))`, the error actually states that it's a `EdgeDataView` object whatever that is – EdChum Nov 16 '17 at 09:17
  • Possible duplicate of [In Python, what does it mean if an object is subscriptable or not?](https://stackoverflow.com/questions/216972/in-python-what-does-it-mean-if-an-object-is-subscriptable-or-not) – Nabin Nov 16 '17 at 09:19
  • Not a duplicate - this is an issue that networkx v2.0 has different data types than v1.1, so the reason for this error is the change. Look at the [migration guide](https://networkx.github.io/documentation/latest/release/migration_guide_from_1.x_to_2.0.html) for how to updata v1.x code to v2.0 – Joel Nov 16 '17 at 15:59

1 Answers1

14

The tutorial is based on a previous version of networkx where g.edges() or g.edges(Data=True) would give you a list of tuples. Lists are subscriptable.

The version you are running has a different output, g.edges gives you an EdgeView property, and g.edges(data=True) an EdgeDataView object which are not subscriptable. To answer your question, you can do:

list(g.edges(data=True))[0:5]

Note: the same is true for g.nodes(): before it was a list now it's a NodeView property not subscriptable. So don't forget to transform it to a list object before trying to add subscripts ([x:x]).

rodgdor
  • 2,530
  • 1
  • 19
  • 26
  • 1
    It's worth linking to the [migration guide](https://networkx.github.io/documentation/latest/release/migration_guide_from_1.x_to_2.0.html) for any answers where the problem is caused by the change from 1.x to 2.0 – Joel Nov 16 '17 at 16:00