This repository has been archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
grouping.jl
175 lines (143 loc) · 6.53 KB
/
grouping.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module TestGrouping
using Base.Test
using DataTables
srand(1)
dt = DataTable(a = repeat([1, 2, 3, 4], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
#dt[6, :a] = Nullable()
#dt[7, :b] = Nullable()
nullfree = DataTable(Any[collect(1:10)], [:x1])
@testset "colwise" begin
@testset "::Function, ::AbstractDataTable" begin
cw = colwise(sum, dt)
answer = NullableArray([20, 12, -0.4283098098931877])
@test isa(cw, NullableArray{Any, 1})
@test size(cw) == (ncol(dt),)
@test isequal(cw, answer)
cw = colwise(sum, nullfree)
answer = [55]
@test isa(cw, Array{Int, 1})
@test size(cw) == (ncol(nullfree),)
@test cw == answer
end
@testset "::Function, ::GroupedDataTable" begin
gd = groupby(DataTable(A = [:A, :A, :B, :B], B = 1:4), :A)
@test colwise(length, gd) == [[2,2], [2,2]]
end
@testset "::Vector, ::AbstractDataTable" begin
cw = colwise([sum], dt)
answer = NullableArray([20 12 -0.4283098098931877])
@test isa(cw, NullableArray{Any, 2})
@test size(cw) == (length([sum]),ncol(dt))
@test isequal(cw, answer)
cw = colwise([sum, minimum], nullfree)
answer = reshape([55, 1], (2,1))
@test isa(cw, Array{Int, 2})
@test size(cw) == (length([sum, minimum]), ncol(nullfree))
@test cw == answer
cw = colwise([NullableArray], nullfree)
answer = reshape([NullableArray(1:10)], (1,1))
@test isa(cw, Array{NullableArray{Int,1},2})
@test size(cw) == (length([NullableArray]), ncol(nullfree))
@test isequal(cw, answer)
@test_throws MethodError colwise(["Bob", :Susie], DataTable(A = 1:10, B = 11:20))
end
@testset "::Vector, ::GroupedDataTable" begin
gd = groupby(DataTable(A = [:A, :A, :B, :B], B = 1:4), :A)
@test colwise([length], gd) == [[2 2], [2 2]]
end
@testset "::Tuple, ::AbstractDataTable" begin
cw = colwise((sum, length), dt)
answer = Any[Nullable(20) Nullable(12) Nullable(-0.4283098098931877); 8 8 8]
@test isa(cw, Array{Any, 2})
@test size(cw) == (length((sum, length)), ncol(dt))
@test isequal(cw, answer)
cw = colwise((sum, length), nullfree)
answer = reshape([55, 10], (2,1))
@test isa(cw, Array{Int, 2})
@test size(cw) == (length((sum, length)), ncol(nullfree))
@test cw == answer
cw = colwise((CategoricalArray, NullableArray), nullfree)
answer = reshape([CategoricalArray(1:10), NullableArray(1:10)],
(length((CategoricalArray, NullableArray)), ncol(nullfree)))
@test typeof(cw) == Array{AbstractVector,2}
@test size(cw) == (length((CategoricalArray, NullableArray)), ncol(nullfree))
@test isequal(cw, answer)
@test_throws MethodError colwise(("Bob", :Susie), DataTable(A = 1:10, B = 11:20))
end
@testset "::Tuple, ::GroupedDataTable" begin
gd = groupby(DataTable(A = [:A, :A, :B, :B], B = 1:4), :A)
@test colwise((length), gd) == [[2,2],[2,2]]
end
@testset "::Function" begin
cw = map(colwise(sum), (nullfree, dt))
answer = ([55], NullableArray(Any[20, 12, -0.4283098098931877]))
@test isequal(cw, answer)
cw = map(colwise((sum, length)), (nullfree, dt))
answer = (reshape([55, 10], (2,1)), Any[Nullable(20) Nullable(12) Nullable(-0.4283098098931877); 8 8 8])
@test isequal(cw, answer)
cw = map(colwise([sum, length]), (nullfree, dt))
@test isequal(cw, answer)
end
end
cols = [:a, :b]
f(dt) = DataTable(cmax = maximum(dt[:c]))
sdt = unique(dt[cols])
# by() without groups sorting
bdt = by(dt, cols, f)
@test bdt[cols] == sdt
# by() with groups sorting
sbdt = by(dt, cols, f, sort=true)
@test sbdt[cols] == sort(sdt)
byf = by(dt, :a, dt -> DataTable(bsum = sum(dt[:b])))
# groupby() without groups sorting
gd = groupby(dt, cols)
ga = map(f, gd)
@test isequal(bdt, combine(ga))
# groupby() with groups sorting
gd = groupby(dt, cols, sort=true)
ga = map(f, gd)
@test sbdt == combine(ga)
g(dt) = DataTable(cmax1 = Vector(dt[:cmax]) + 1)
h(dt) = g(f(dt))
@test isequal(combine(map(h, gd)), combine(map(g, ga)))
# testing pool overflow
dt2 = DataTable(v1 = categorical(collect(1:1000)), v2 = categorical(fill(1, 1000)))
@test groupby(dt2, [:v1, :v2]).starts == collect(1:1000)
@test groupby(dt2, [:v2, :v1]).starts == collect(1:1000)
# grouping empty table
@test groupby(DataTable(A=Int[]), :A).starts == Int[]
# grouping single row
@test groupby(DataTable(A=Int[1]), :A).starts == Int[1]
# issue #960
x = CategoricalArray(collect(1:20))
dt = DataTable(v1=x, v2=x)
groupby(dt, [:v1, :v2])
dt2 = by(e->1, DataTable(x=Int64[]), :x)
@test size(dt2) == (0,1)
@test isequal(sum(dt2[:x]), Nullable(0))
# Check that reordering levels does not confuse groupby
dt = DataTable(Key1 = CategoricalArray(["A", "A", "B", "B"]),
Key2 = CategoricalArray(["A", "B", "A", "B"]),
Value = 1:4)
gd = groupby(dt, :Key1)
@test isequal(gd[1], DataTable(Key1=["A", "A"], Key2=["A", "B"], Value=1:2))
@test isequal(gd[2], DataTable(Key1=["B", "B"], Key2=["A", "B"], Value=3:4))
gd = groupby(dt, [:Key1, :Key2])
@test isequal(gd[1], DataTable(Key1="A", Key2="A", Value=1))
@test isequal(gd[2], DataTable(Key1="A", Key2="B", Value=2))
@test isequal(gd[3], DataTable(Key1="B", Key2="A", Value=3))
@test isequal(gd[4], DataTable(Key1="B", Key2="B", Value=4))
# Reorder levels, add unused level
levels!(dt[:Key1], ["Z", "B", "A"])
levels!(dt[:Key2], ["Z", "B", "A"])
gd = groupby(dt, :Key1)
@test isequal(gd[1], DataTable(Key1=["A", "A"], Key2=["A", "B"], Value=1:2))
@test isequal(gd[2], DataTable(Key1=["B", "B"], Key2=["A", "B"], Value=3:4))
gd = groupby(dt, [:Key1, :Key2])
@test isequal(gd[1], DataTable(Key1="A", Key2="A", Value=1))
@test isequal(gd[2], DataTable(Key1="A", Key2="B", Value=2))
@test isequal(gd[3], DataTable(Key1="B", Key2="A", Value=3))
@test isequal(gd[4], DataTable(Key1="B", Key2="B", Value=4))
end