pandas
"Functions to read and write graphs from and to pandas DataFrames.
add_edge_attributes
¶
Add (temporal) edge attributes from pandas.DataFrame to existing graph.
Edge attributes are mapped based on source/target node IDs in columns v and w,
and edge attributes x are given in columns edge_x.
If time_attr is not None, the dataframe is expected to contain temporal data with a timestamp
in a column named as specified in time_attr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
A DataFrame with rows containing edges and optional edge attributes. |
required |
g
|
pathpyG.core.graph.Graph
|
The graph to which the edge attributes should be added. |
required |
time_attr
|
str | None
|
If not None, the name of the column containing time stamps for temporal edges. |
None
|
Source code in src/pathpyG/io/pandas.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
add_node_attributes
¶
Add node attributes from pandas.DataFrame to existing graph.
Node IDs or indices are given in column v and node attributes x are given in columns node_x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
A DataFrame with rows containing nodes and optional node attributes. |
required |
g
|
pathpyG.core.graph.Graph
|
The graph to which the node attributes should be added. |
required |
Source code in src/pathpyG/io/pandas.py
df_to_graph
¶
Reads a network from a pandas.DataFrame.
The data frame is expected to have a minimum of two columns that give the source and target nodes of edges. Additional columns in the data frame will be mapped to edge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
A data frame with rows containing edges and optional edge attributes. If the data frame contains column names, the source and target columns must be called 'v' and 'w' respectively. If no column names are used the first two columns are interpreted as source and target. |
required |
is_undirected
|
bool
|
Whether or not to interpret edges as undirected. |
False
|
multiedges
|
bool
|
Whether or not to allow multiple edges between the same node pair. By default multi edges are ignored. |
False
|
num_nodes
|
int | None
|
The number of nodes in the graph. If None, the number of unique nodes in the data frame is used. |
None
|
Example
Source code in src/pathpyG/io/pandas.py
df_to_temporal_graph
¶
Read a temporal graph from a DataFrame.
The DataFrame is expected to have a minimum of two columns v and w
that give the source and target nodes of edges. Each row in the DataFrame is
mapped to one temporal edge. Additional columns in the DataFrame will be
mapped to edge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
pandas.DataFrame with rows containing time-stamped edges and optional edge attributes. |
required |
multiedges
|
bool
|
Whether or not to allow multiple edges between the same node pair. By default multi edges are ignored. |
False
|
timestamp_format
|
The format of the time stamps in the |
'%Y-%m-%d %H:%M:%S'
|
|
time_rescale
|
The factor by which to rescale the time stamps. Defaults to 1, meaning no rescaling. |
1
|
|
num_nodes
|
int | None
|
The number of nodes in the graph. If None, the number of unique nodes in the DataFrame is used. |
None
|
Example
Source code in src/pathpyG/io/pandas.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
graph_to_df
¶
Return a pandas.DataFrame for a given graph.
Contains all edges including edge attributes. Node and network-level attributes are not included. To facilitate the import into network analysis tools that only support integer node identifiers, node uids can be replaced by a consecutive, zero-based index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
The graph to export as pandas DataFrame |
required |
node_indices
|
typing.Optional[bool]
|
whether nodes should be exported as integer indices |
False
|
Example
Source code in src/pathpyG/io/pandas.py
read_csv_graph
¶
Read a Graph from a csv file.
This method reads a graph from a .csv-file and converts it to a
Graph object. To read a temporal graph, the csv file must have
a header with column t containing time stamps of edges
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the csv file containing the graph data. |
required |
sep
|
str
|
character separating columns in the csv file |
','
|
header
|
bool
|
whether or not the first line of the csv file is interpreted as header with column names |
True
|
is_undirected
|
bool
|
whether or not to interpret edges as undirected |
False
|
multiedges
|
bool
|
whether or not to allow multiple edges between the same node pair. By default multi edges are ignored. |
False
|
**kwargs
|
typing.Any
|
Additional keyword arguments passed to the |
{}
|
Example
Source code in src/pathpyG/io/pandas.py
read_csv_path_data
¶
Read multiple paths stored in an n-gram csv file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_buf
|
typing.Any
|
File, path or file-like object that the pandas.read_table function will read from |
None
|
weight
|
bool
|
If True the last column of each row in the CSV file will be interpreted as a count or weight |
True
|
sep
|
character that separates the nodes (and weight) in each line of the input file |
','
|
|
device
|
typing.Optional[torch.device]
|
The device on which the PathData object should be created |
None
|
Source code in src/pathpyG/io/pandas.py
read_csv_temporal_graph
¶
Read a TemporalGraph from a csv file.
This method reads a temporal graph from a .csv-file and converts it to a
TemporalGraph object. The csv file is expected to have a header with columns
v, w, and t containing source nodes, target nodes, and time stamps of edges,
respectively. Additional columns in the csv file will be interpreted as edge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the csv file containing the temporal graph data. |
required |
sep
|
str
|
character separating columns in the csv file |
','
|
header
|
bool
|
whether or not the first line of the csv file is interpreted as header with column names |
True
|
timestamp_format
|
str
|
The format of the time stamps in the |
'%Y-%m-%d %H:%M:%S'
|
time_rescale
|
int
|
The factor by which to rescale the time stamps. Defaults to 1, meaning no rescaling. |
1
|
**kwargs
|
typing.Any
|
Additional keyword arguments passed to the |
{}
|
Source code in src/pathpyG/io/pandas.py
temporal_graph_to_df
¶
Return a pandas.DataFrame for a given temporal graph.
Contains all edges including edge attributes. Node and network-level attributes are not included. To facilitate the import into network analysis tools that only support integer node identifiers, node uids can be replaced by a consecutive, zero-based index.
facilitate the import into network analysis tools that only support integer node identifiers, node uids can be replaced by a consecutive, zero-based index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.temporal_graph.TemporalGraph
|
The graph to export as pandas DataFrame |
required |
node_indices
|
typing.Optional[bool]
|
whether nodes should be exported as integer indices |
False
|
Example
Source code in src/pathpyG/io/pandas.py
write_csv
¶
Store all edges of a graph or temporal graph in a csv file.
This method stores a Graph or TemporalGraph as a .csv file. The csv file
will contain all edges including edge attributes. Node and network-level attributes
are not included. To facilitate the import into network analysis tools that only
support integer node identifiers, node uids can be replaced by a consecutive,
zero-based index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
typing.Union[pathpyG.core.graph.Graph, pathpyG.core.temporal_graph.TemporalGraph]
|
The graph to export as pandas DataFrame |
required |
node_indices
|
bool
|
whether nodes should be exported as integer indices |
False
|
path_or_buf
|
typing.Any
|
String, path, or file-like object (see documentation of pandas.DataFrame.to_csv) |
None
|
**pdargs
|
typing.Any
|
Additional keyword arguments passed to pandas.DataFrame.to_csv. |
{}
|