Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/DataFrame.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import DataFrames.DataFrame

"""
DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph

Construct a DataFrame from a MetaGraph from either its node or edge properties.

`gr` is a MetaGraph.

Optional keyword arguments:

`type` is a Symbol valued either :node or :edge such that the DataFrame is populated with node or edge
properties stored in `gr`. Default is :node.

"""
function DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph
fl, prps, en, nu = if type == :node
:node => Int[], gr.vprops, vertices, nv
elseif type == :edge
:edge => Edge[], gr.eprops, edges, ne
else
error("specify type equal to :node or :edge")
end

dx = DataFrame(fl)

x = unique(reduce(vcat, values(prps)))
for y in x
for (k, v) in y
dx[!, k] = typeof(v)[] # may be a problem if type is not consistent in y?
allowmissing!(dx, k)
end
end

dx = similar(dx, nu(gr))

for (i, e) in (enumerate∘en)(gr)
dx[i, type] = e
pr = props(gr, e)
for (nme, val) in pr
dx[i, nme] = val
end
end

# remove missing when possible
for v in Symbol.(names(dx))
if !any(ismissing.(dx[!, v]))
disallowmissing!(dx, v)
end
end
return dx
end
2 changes: 2 additions & 0 deletions src/GraphDataFrameBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export MetaGraph, MetaDiGraph
import MetaGraphs.MetaGraph
import MetaGraphs.MetaDiGraph

include("DataFrame.jl")
export DataFrame

function MetaGraph(
df::T,
Expand Down