Skip to content

Support pandas "Int64" type #607

@Adriankhl

Description

@Adriankhl

What is the feature or improvement you would like to see?
This produces an error:

import pandas as pd
import numpy as np
import igraph as ig

df = pd.DataFrame(np.array([[0, 1], [1, 1]]), columns=['a', 'b'])
df["a"] = df["a"].astype('Int64')
g = ig.Graph.DataFrame(df)

The error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [19], line 1
----> 1 g = ig.Graph.DataFrame(df)

File ~/.pyenv/versions/3.11.0/lib/python3.11/site-packages/igraph/io/objects.py:424, in _construct_graph_from_dataframe(cls, edges, directed, vertices, use_vids)
    419 if use_vids:
    420     if not (
    421         str(edges.dtypes[0]).startswith("int")
    422         and str(edges.dtypes[1]).startswith("int")
    423     ):
--> 424         raise TypeError(
    425             f"Source and target IDs must be 0-based integers, found types {edges.dtypes.tolist()[:2]}"
    426         )
    427     elif (edges.iloc[:, :2] < 0).any(axis=None):
    428         raise ValueError("Source and target IDs must not be negative")

TypeError: Source and target IDs must be 0-based integers, found types [Int64Dtype(), dtype('int64')]

Int64 is a nullable pandas int data type. I believe the type check is too strict. As long as the Int64 does not contain any null value, the dataframe should be able to convert to an igraph object. The error message is also not precise for this case.

Use cases for the feature
Sometimes we just have to deal with a column with Int64 as the data type, it adds a bit of annoyance if we have to convert it to int64 every time.

References
https://pandas.pydata.org/docs/user_guide/integer_na.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    wishlistFeature request that has not been chosen for implementation yet; vote or comment to prioritize it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions