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

Fix handling of variable_eltype in stack #3043

Merged
merged 4 commits into from
May 2, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ DataAPI = "1.10"
InvertedIndices = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
Missings = "0.4.2, 1"
PooledArrays = "1.3.0"
PooledArrays = "1.4.2"
PrettyTables = "0.12, 1"
Reexport = "0.1, 0.2, 1"
ShiftedArrays = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/abstractdataframe/reshape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function stack(df::AbstractDataFrame,
# (note that copyto! inserts levels in their order of appearance)
nms = names(df, ints_measure_vars)
simnms = similar(nms, variable_eltype)
catnms = simnms isa Vector ? PooledArray(catnms) : simnms
catnms = simnms isa Vector ? PooledArray(simnms) : simnms
copyto!(catnms, nms)
end
return DataFrame(AbstractVector[[repeat(df[!, c], outer=N) for c in ints_id_vars]..., # id_var columns
Expand Down
14 changes: 14 additions & 0 deletions test/reshape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -845,4 +845,18 @@ end
end
end

@testset "variable_eltype in stack tests" begin
df = DataFrame(A=1:3, B=[2.0, -1.1, 2.8], C=["p", "q", "r"])
@test_throws MethodError stack(df, :C, variable_name=:D, variable_eltype=Int)
for T in (AbstractString, Any)
sdf = stack(df, [:A, :B], variable_name=:D, variable_eltype=T)
@test sdf == DataFrame(C=["p", "q", "r", "p", "q", "r"],
D=["A", "A", "A", "B", "B", "B"],
value=[1.0, 2.0, 3.0, 2.0, -1.1, 2.8])
@test sdf.C isa Vector{String}
@test sdf.value isa Vector{Float64}
@test sdf.D isa PooledVector{T}
end
end

end # module