Skip to content

Commit

Permalink
don't assume groups are non-empty (#991)
Browse files Browse the repository at this point in the history
grouping on an empty data frame should not crash. combine can't create additional columns but returning an empty data frame is better than crashing.



Similarly, showing an GroupedDataFrame in the REPL should not assume that it's non-empty.
  • Loading branch information
gustafsson authored and nalimilan committed Aug 26, 2016
1 parent e80dbef commit 73d6363
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/groupeddataframe/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function combine(ga::GroupApplied)
gd, vals = ga.gd, ga.vals
# Could be made shorter with a rep(x, lengths) function
# See JuliaLang/julia#16443
idx = Vector{Int}(sum([size(val, 1) for val in vals]))
idx = Vector{Int}(sum(Int[size(val, 1) for val in vals]))
j = 0
for i in 1:length(vals)
n = size(vals[i], 1)
Expand Down
6 changes: 4 additions & 2 deletions src/groupeddataframe/show.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
function Base.show(io::IO, gd::GroupedDataFrame)
N = length(gd)
println(io, "$(typeof(gd)) $N groups with keys: $(gd.cols)")
println(io, "First Group:")
show(io, gd[1])
if N > 0
println(io, "First Group:")
show(io, gd[1])
end
if N > 1
print(io, "\n\n")
println(io, "Last Group:")
Expand Down
4 changes: 4 additions & 0 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ module TestGrouping
x = pool(collect(1:20))
df = DataFrame(v1=x, v2=x)
groupby(df, [:v1, :v2])

df2 = by(e->1, DataFrame(x=Int64[]), :x)
@test size(df2) == (0,1)
@test sum(df2[:x]) == 0
end

0 comments on commit 73d6363

Please sign in to comment.