Skip to content

feat: graph_from_data_frame(vertex_ids = TRUE) reads numeric vertex IDs, so as_data_frame(what = "both") round-trips unnamed graphs - #2923

Open
schochastics wants to merge 2 commits into
mainfrom
f-223-vertex-ids
Open

schochastics wants to merge 2 commits into
mainfrom
f-223-vertex-ids

Conversation

@schochastics

Copy link
Copy Markdown
Contributor

Closes #223.

Until now, graph_from_data_frame() always read vertices[, 1] as vertex names and converted the endpoints to strings. That meant as_data_frame(g, what = "both") could not be passed back in for graphs without vertex names. This PR adds an opt-in keyword argument:

df <- as_data_frame(g, what = "both")
graph_from_data_frame(df$edges, vertices = df$vertices, vertex_ids = TRUE)

With vertex_ids = TRUE:

  • The first two columns of d must be positive whole numbers. They are used as vertex IDs.
  • Every column of vertices becomes a vertex attribute, one row per vertex in ID order. No name attribute is created unless vertices has a name column.
  • If vertices is NULL, the vertex count is the largest ID in d, so isolated vertices up to that ID are kept.

The default (vertex_ids = FALSE) behaves as before. In addition:

  • The "Some vertex names in d are not listed in vertices" error now suggests vertex_ids = TRUE when the endpoints are in-range integers.
  • A zero-column vertices used to fail with an internal [.data.frame error. It now reports "vertices contains no columns" with the same hint. as_data_frame() returns exactly this for an unnamed graph without vertex attributes.

Notes:

  • vertex_ids is also added to the new signature in tools/migrations/conversion.R, and the ARG_HANDLE block was regenerated. As a result, the abbreviations v, ve, ver and vert now abort as ambiguous. Before, they were recovered as vertices with a deprecation warning.
  • Graph-level attributes (such as make_ring()'s name) are not stored in the data frames, so the round trip is exact only for vertex and edge data.

🤖 Generated with Claude Code

schochastics and others added 2 commits September 29, 2026 21:25
… IDs, so `as_data_frame(what = "both")` round-trips unnamed graphs

Closes #223.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if dde9886 is merged into main:

  • ✔️as_adjacency_matrix: 127ms -> 127ms [-1.13%, +1.8%]
  • ✔️as_biadjacency_matrix: 136ms -> 136ms [-1.89%, +1.48%]
  • ✔️as_data_frame_both: 146ms -> 146ms [-0.72%, +1.75%]
  • ✔️as_long_data_frame: 119ms -> 120ms [-0.58%, +2.9%]
  • ✔️es_attr_assign: 127ms -> 128ms [-0.99%, +2.53%]
  • ✔️es_attr_filter: 137ms -> 137ms [-0.65%, +0.82%]
  • ✔️graph_from_adjacency_matrix: 161ms -> 161ms [-1.14%, +1.65%]
  • ✔️graph_from_data_frame: 159ms -> 160ms [-0.51%, +1.75%]
  • ✔️incident_edges: 143ms -> 142ms [-3.07%, +1.82%]
  • ✔️neighbors_by_name: 140ms -> 140ms [-1.73%, +1.21%]
  • ✔️union_named: 138ms -> 139ms [-1.86%, +2.58%]
  • ✔️vs_attr_filter: 166ms -> 167ms [-0.25%, +0.74%]
  • ✔️vs_by_name: 147ms -> 147ms [-0.17%, +1.01%]
    Further explanation regarding interpretation and methodology can be found in the documentation.

@schochastics
schochastics requested a review from maelle September 30, 2026 05:55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better cooperation of as_data_frame() and graph_from_data_frame() ?

1 participant