Skip to content

Commit

Permalink
Work around crash
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Oct 17, 2018
1 parent 15fe9b0 commit 4e7f044
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/groupeddataframe/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ end
"for all groups (got $N and $(length(row)))"))
end
@inbounds for j in colstart:length(cols)
val = row[j]
col = cols[j]
cn = colnames[j]
local val
Expand Down Expand Up @@ -347,6 +346,13 @@ function _combine!(first::Union{NamedTuple, DataFrameRow}, cols::NTuple{N, Abstr
cols
end

# This needs to be in a separate function
# to work around a crash due to JuliaLang/julia#29430
@noinline function do_append!(do_it, col, vals)
do_it && append!(col, vals)
return do_it
end

function append_rows!(rows, cols::NTuple{N, AbstractVector},
colstart::Integer, colnames::AbstractVector{Symbol}) where N
if !isa(rows, AbstractDataFrame)
Expand All @@ -357,7 +363,6 @@ function append_rows!(rows, cols::NTuple{N, AbstractVector},
"for all groups (got $N and $(size(rows, 2)))"))
end
@inbounds for j in colstart:length(cols)
vals = rows[j]
col = cols[j]
cn = colnames[j]
local vals
Expand All @@ -369,9 +374,7 @@ function append_rows!(rows, cols::NTuple{N, AbstractVector},
end
S = eltype(vals)
T = eltype(col)
if S <: T || promote_type(S, T) <: T
append!(col, vals)
else
if !do_append!(S <: T || promote_type(S, T) <: T, col, vals)
return j
end
end
Expand Down
16 changes: 7 additions & 9 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,19 @@ module TestGrouping
@test gd[3] == DataFrame(Key1="B", Key2="A", Value=3)
@test gd[4] == DataFrame(Key1="B", Key2="B", Value=4)

# TODO: uncomment once JuliaLang/julia#29430 is fixed
# Check that CategoricalArray column is preserved when returning a value...
# res = combine(d -> DataFrame(x=d[1, :Key2]), groupby(df, :Key1))
# @test res.x isa CategoricalVector{String}
res = combine(d -> DataFrame(x=d[1, :Key2]), groupby(df, :Key1))
@test res.x isa CategoricalVector{String}
res = combine(d -> (x=d[1, :Key2],), groupby(df, :Key1))
@test res.x isa CategoricalVector{String}
# ...and when returning an array
# res = combine(d -> DataFrame(x=d[:Key1]), groupby(df, :Key1))
# @test res.x isa CategoricalVector{String}
res = combine(d -> DataFrame(x=d[:Key1]), groupby(df, :Key1))
@test res.x isa CategoricalVector{String}

# TODO: uncomment once JuliaLang/julia#29430 is fixed
# Check that CategoricalArray and String give a String...
# res = combine(d -> d.Key1 == ["A", "A"] ? DataFrame(x=d[1, :Key1]) : DataFrame(x="C"),
# groupby(df, :Key1))
# @test res.x isa Vector{String}
res = combine(d -> d.Key1 == ["A", "A"] ? DataFrame(x=d[1, :Key1]) : DataFrame(x="C"),
groupby(df, :Key1))
@test res.x isa Vector{String}
res = combine(d -> d.Key1 == ["A", "A"] ? (x=d[1, :Key1],) : (x="C",),
groupby(df, :Key1))
@test res.x isa Vector{String}
Expand Down

0 comments on commit 4e7f044

Please sign in to comment.