Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added makeunique argument for Table -> DataFrame #2278

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 8 additions & 7 deletions src/other/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,29 @@ Tables.getcolumn(dfr::DataFrameRow, nm::Symbol) = dfr[nm]
getvector(x::AbstractVector) = x
getvector(x) = [x[i] for i = 1:length(x)]
# note that copycols is ignored in this definition (Tables.CopiedColumns implies copies have already been made)
fromcolumns(x::Tables.CopiedColumns, names; copycols::Bool=true) =
fromcolumns(x::Tables.CopiedColumns, names; makeunique::Bool=false, copycols::Bool=true) =
DataFrame(AbstractVector[getvector(Tables.getcolumn(x, nm)) for nm in names],
Index(names),
Index(names, makeunique=makeunique),
copycols=false)
fromcolumns(x, names; copycols::Bool=true) =
fromcolumns(x, names; makeunique::Bool=false, copycols::Bool=true) =
DataFrame(AbstractVector[getvector(Tables.getcolumn(x, nm)) for nm in names],
Index(names),
Index(names, makeunique=makeunique),
copycols=copycols)

function DataFrame(x::T; copycols::Bool=true) where {T}
function DataFrame(x::T; makeunique::Bool=false, copycols::Bool=true) where {T}
if x isa AbstractVector && all(col -> isa(col, AbstractVector), x)
return DataFrame(Vector{AbstractVector}(x), copycols=copycols)
return DataFrame(Vector{AbstractVector}(x), makeunique=makeunique, copycols=copycols)
end
if x isa AbstractVector || x isa Tuple
if all(v -> v isa Pair{Symbol, <:AbstractVector}, x)
return DataFrame(AbstractVector[last(v) for v in x], [first(v) for v in x],
makeunique=makeunique,
copycols=copycols)
end
end
cols = Tables.columns(x)
names = collect(Symbol, Tables.columnnames(cols))
return fromcolumns(cols, names, copycols=copycols)
return fromcolumns(cols, names, makeunique=makeunique, copycols=copycols)
end

function Base.append!(df::DataFrame, table; cols::Symbol=:setequal,
Expand Down