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

Respect copycols when building DataFrame from Tables.CopiedColumns #2656

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 6 additions & 5 deletions src/other/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ 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) =
DataFrame(AbstractVector[getvector(Tables.getcolumn(x, nm)) for nm in names],
Index(names),
copycols=false)

fromcolumns(x, names; copycols::Bool=true) =
DataFrame(AbstractVector[getvector(Tables.getcolumn(x, nm)) for nm in names],
Index(names),
copycols=copycols)

# note that copycols is false by default in this definition (Tables.CopiedColumns implies copies have already been made)
# but if `copycols=true`, a copy will still be made; this is useful for scenarios where the input is immutable
# so avoiding copies is desirable, but you may still want a copy for mutation (Arrow.Table is like this)
DataFrame(x::Tables.CopiedColumns; copycols::Bool=false) = DataFrame(Tables.source(x); copycols=copycols)
bkamins marked this conversation as resolved.
Show resolved Hide resolved

function DataFrame(x::T; copycols::Bool=true) where {T}
if !Tables.istable(x) && x isa AbstractVector && !isempty(x)
# here we handle eltypes not specific enough to be dispatched
Expand Down
5 changes: 5 additions & 0 deletions test/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ end
@test Tables.columntable(df3) == nt
@test Tables.columntable(df3) !== nt

df4 = DataFrame(Tables.CopiedColumns(nt))
@test df4.a === nt.a
df4 = DataFrame(Tables.CopiedColumns(nt), copycols=true)
@test df4.a !== nt.a

v = [(a=1, b=2), (a=3, b=4)]
df = DataFrame(v)
@test size(df) == (2, 2)
Expand Down