Skip to content

Commit d1eed4e

Browse files
authored
Groupby reduce with string grouping keys fix (#24)
``` a = DTable((a=rand(["abc", "bcd", "efg"], 100), b=rand(100)), 11) r = fetch(reduce((x, y) -> x + y, DTables.groupby(a, :a), cols=[:b], init=0)) ``` it would return `r.a = ['a', 'b', 'e']`
1 parent 39ef120 commit d1eed4e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/operations/operations.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,12 @@ function reduce(
196196
]
197197

198198
construct_result =
199-
(_keys, _gcols, _columns, _results, _prefix) -> begin
200-
ks = [col => getindex.(_keys, i) for (i, col) in enumerate(_gcols)]
199+
(_keys::Base.KeySet, _gcols, _columns, _results, _prefix) -> begin
200+
ks = if eltype(_keys) <: NamedTuple # many keys in groupby
201+
[col => getindex.(_keys, col) for col in _gcols]
202+
else # single key in groupby
203+
[col => collect(_keys) for col in _gcols]
204+
end
201205
rs = [
202206
Symbol(_prefix * string(r)) => fetch(_results[i]) for (i, r) in enumerate(_columns)
203207
]

test/table.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,15 @@ using OnlineStats
435435
r = reduce(*, g)
436436
fr = fetch(r)
437437

438+
a = DTable((a=rand(["abc", "bcd", "efg"], 100), b=rand(100)), 11)
439+
r = fetch(reduce((x, y) -> x + y, DTables.groupby(a, :a), cols=[:b], init=0))
440+
@test all(a.a .∈ Ref(r.a))
441+
442+
a = DTable((a=rand(["abc", "bcd", "efg"], 100), b=rand(100)), 11)
443+
r = fetch(reduce((x, y) -> x + y, DTables.groupby(a, [:a, :b]), cols=[:b], init=0))
444+
@test all(a.a .∈ Ref(r.a))
445+
@test all(a.b .∈ Ref(r.b))
446+
438447
for (i, key) in enumerate(fr.a)
439448
@test fr.result_a[i] == repeat(key, length(cs1) ÷ 4)
440449
@test fr.result_b[i] == 3 ^ (length(cs1) ÷ 4)

0 commit comments

Comments
 (0)