Skip to content

Commit

Permalink
Merge a52f0a8 into 72a6304
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Jun 14, 2018
2 parents 72a6304 + a52f0a8 commit dc2f87c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/groupeddataframe/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ combine(map(d -> mean(skipmissing(d[:c])), gd))
```
"""
function groupby(df::AbstractDataFrame, cols::Vector{T};
sort::Bool = false, skipmissing::Bool = false) where T
function groupby(df::AbstractDataFrame, cols::Vector;
sort::Bool = false, skipmissing::Bool = false)
sdf = df[cols]
df_groups = group_rows(sdf, skipmissing)
# sort the groups
Expand Down Expand Up @@ -345,7 +345,7 @@ end
# Applies aggregate to non-key cols of each SubDataFrame of a GroupedDataFrame
aggregate(gd::GroupedDataFrame, f::Function; sort::Bool=false) = aggregate(gd, [f], sort=sort)
function aggregate(gd::GroupedDataFrame, fs::Vector{T}; sort::Bool=false) where T<:Function
headers = _makeheaders(fs, setdiff(_names(gd), gd.cols))
headers = _makeheaders(fs, setdiff(_names(gd), _names(gd.parent[gd.cols])))
res = combine(map(x -> _aggregate(without(x, gd.cols), fs, headers), gd))
sort && sort!(res, headers)
res
Expand Down
8 changes: 8 additions & 0 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,21 @@ module TestData
@test sum(df8[:d1_length]) == N
@test all(df8[:d1_length] .> 0)
@test df8[:d1_length] == [sum(isequal.(d2, "A")), sum(isequal.(d2, "B")), sum(ismissing.(d2))]
df8′ = aggregate(df7, 2, [sum, length], sort=true)
@test df8 df8′
adf = aggregate(groupby(df7, :d2, sort=true), [sum, length])
@test df8 adf
adf′ = aggregate(groupby(df7, 2, sort=true), [sum, length])
@test df8 adf′
adf = aggregate(groupby(df7, :d2), [sum, length], sort=true)
@test sort(df8, [:d1_sum, :d3_sum, :d1_length, :d3_length]) adf
adf′ = aggregate(groupby(df7, 2), [sum, length], sort=true)
@test adf adf′

df9 = aggregate(df7, :d2, [sum, length], sort=true)
@test df9 df8
df9′ = aggregate(df7, 2, [sum, length], sort=true)
@test df9′ df8

df10 = DataFrame(
Any[[1:4;], [2:5;], ["a", "a", "a", "b" ], ["c", "d", "c", "d"]],
Expand Down

0 comments on commit dc2f87c

Please sign in to comment.