'Python TypeError when creating a bipartite graph with igraph

I am trying to create an bipartite graph with igraph in Python, but there is something wrong with my code and I don't know where.

I have an edge dataframe with two columns where the edges are indicate, and a list with the types of the nodes.

ed_tup = edges_inc[['id_proveedor','id_inc_ue']].to_records(index=False)
ed_list = list(ed_tup)

t=list()
p=list()
uc=list()
for i, row in edges_inc[['id_proveedor','id_inc_ue']].iterrows():
  if row['id_proveedor'] in p:
    pass
  else:
    p.append(row['id_proveedor'])
    t.append(str('id_proveedor'))
  if row['id_inc_ue'] in uc:
    pass
  else:
    uc.append(row['id_inc_ue'])
    t.append(str('id_inc_ue')
  n=n+1

g_buy=ig.Graph.Bipartite(t, edges=ed_list,directed=True)

The error is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-8c2b2b51467a> in <module>()
      2 ed_list = list(ed_tup)
      3 #print(ed_list)
----> 4 g_buy=ig.Graph.Bipartite(t, edges=ed_list,directed=True)

/usr/local/lib/python3.7/dist-packages/igraph/__init__.py in Bipartite(cls, types, edges, directed, *args, **kwds)
   3257           stores the vertex classes.
   3258         """
-> 3259         result = cls._Bipartite(types, edges, directed, *args, **kwds)
   3260         result.vs["type"] = [bool(x) for x in types]
   3261         return result

TypeError: only numbers, strings or igraph.Vertex objects can be converted to vertex IDs

Also, I'm trying to create a classic igraph and add an attribute to the nodes that indicates their type, but I don't know if it's correct. That is, I don't know if it is also a bipartite graph.



Solution 1:[1]

if you check the documentation, types need be a boolean vector, so try the following changes in your dataframe

a,b=pd.factorize(jj['start']) 
c,d=pd.factorize(jj['end'])
rrr=pd.DataFrame()
rrr['start']=a+max(c)+1
rrr['end']=c
ed_tup = rrr[['start','end']].to_records(index=False)

ed_list = list(ed_tup)

t=list()
p=list()
uc=list()
n=0
for i, row in rrr[['start','end']].iterrows():
    if row['start'] in p:
        pass
    else:
        p.append(row['start'])
        t.append(0)
    if row['end'] in uc:
        pass
    else:
        uc.append(row['end'])
        t.append(1)
    n=n+1

Perhaps even easier is just to read in your dataframe directly through igraph, and if you have unique, non-overlapping nodes sets in source and target columns, igraph infers it to be bipartite and defines it as such

mygraph = ig.Graph.DataFrame(jj)
mygraph.is_bipartite()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1