Home > Blockchain >  Create a dynamic graph with DyNetx from dataframe
Create a dynamic graph with DyNetx from dataframe

Time:11-14

I am trying to create a dynamic graph with DyNetx, however, I am having trouble adding nodes. When I try to add a sender, receiver and timestamp with my real data the result is

[None, None, None]

When I tried my code with some sample data, I get the error TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting `n`, use `n * obj.freq`

I'm not sure what the problem is.

My full code is below

import numpy as np
import pandas as pd
from datetime import datetime
import dynetx as dn

df = pd.DataFrame({'sent':['78','18','94','55','68','57','78','8','78','18'],
                   'received':['18','78','35','14','57','68','57','17','18','78'],
                   'time':['2017-06-02 05:41:34','2017-06-02 05:41:35',
                           '2017-06-02 05:41:36','2017-06-02 05:41:37',
                           '2017-06-02 05:41:38','2017-06-02 05:41:39',
                           '2017-06-02 05:41:40','2017-06-02 05:41:41',
                          '2017-06-02 05:41:42','2017-06-02 05:41:43']})
df['time'] = pd.to_datetime(pd.Series(df['time']))


g = dn.DynGraph(edge_removal=True)
[g.add_interaction(df['sent'][i],df['received'][i],df['time'][i]) for i in range(len(df))]

CodePudding user response:

Your error message suggests that timestamps in the date format are no longer supported. I would suggest converting your timestamps to seconds and using those in g.add_interaction. You can do this conversion by using:df['sec']=[pd.Timestamp(df['time'][i]).timestamp() for i in range(len(df))]. More examples of this type of conversion can be found here

So overall the code looks like that:

import numpy as np
import pandas as pd
from datetime import datetime
import dynetx as dn

df = pd.DataFrame({'sent':['78','18','94','55','68','57','78','8','78','18'],
                   'received':['18','78','35','14','57','68','57','17','18','78'],
                   'time':['2017-06-02 05:41:34','2017-06-02 05:41:35',
                           '2017-06-02 05:41:36','2017-06-02 05:41:37',
                           '2017-06-02 05:41:38','2017-06-02 05:41:39',
                           '2017-06-02 05:41:40','2017-06-02 05:41:41',
                          '2017-06-02 05:41:42','2017-06-02 05:41:43']})
g = dn.DynGraph(edge_removal=True)
df['sec']=[pd.Timestamp(df['time'][i]).timestamp() for i in range(len(df))]

[g.add_interaction(df['sent'][i],df['received'][i],df['sec'][i]) for i in range(len(df))]
for e in g.stream_interactions():
  print(e)

And the output of the stream of interactions gives:

('78', '18', ' ', 1496382094.0)
('94', '35', ' ', 1496382096.0)
('55', '14', ' ', 1496382097.0)
('68', '57', ' ', 1496382098.0)
('78', '57', ' ', 1496382100.0)
('8', '17', ' ', 1496382101.0)
('78', '18', ' ', 1496382102.0)
  • Related