Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CategoricalArray fixes #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/freqtable.jl
Expand Up @@ -146,7 +146,14 @@ function _freqtable(x::NTuple{n, AbstractCategoricalVector}, skipmissing::Bool =
len = map(length, x)
miss = map(v -> eltype(v) >: Missing, x)
lev = map(x) do v
eltype(v) >: Missing && !skipmissing ? [levels(v); missing] : allowmissing(levels(v))
pool = CategoricalArrays.pool(v)
invorder = invperm(CategoricalArrays.order(pool))
l = [pool[i] for i in invorder]
if eltype(v) >: Missing
skipmissing ? allowmissing(l) : [l; missing]
else
l
end
end
dims = map(length, lev)
# First entry is for missing values (only correct and used if present)
Expand Down
77 changes: 77 additions & 0 deletions test/freqtable.jl
Expand Up @@ -8,16 +8,20 @@ y = repeat(["D", "C", "A", "B"], inner=[10], outer=[10]);
tab = @inferred freqtable(x)
@test tab == [100, 100, 100, 100]
@test names(tab) == [["a", "b", "c", "d"]]
@test names(tab, 1) isa typeof(x)
@test @inferred prop(tab) == [0.25, 0.25, 0.25, 0.25]
tab = @inferred freqtable(y)
@test tab == [100, 100, 100, 100]
@test names(tab) == [["A", "B", "C", "D"]]
@test names(tab, 1) isa typeof(y)
tab = @inferred freqtable(x, y)
@test tab == [30 20 20 30;
30 20 20 30;
20 30 30 20;
20 30 30 20]
@test names(tab) == [["a", "b", "c", "d"], ["A", "B", "C", "D"]]
@test names(tab, 1) isa typeof(x)
@test names(tab, 2) isa typeof(y)

pt = @inferred prop(tab)
@test pt == [0.075 0.05 0.05 0.075;
Expand Down Expand Up @@ -77,15 +81,35 @@ cy = CategoricalArray(y)
tab = @inferred freqtable(cx)
@test tab == [100, 100, 100, 100]
@test names(tab) == [["a", "b", "c", "d"]]
@test names(tab, 1) isa Array{eltype(cx)}
@test_broken names(tab, 1) isa typeof(cx)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be fixed by JuliaData/CategoricalArrays.jl#252. In practice it shouldn't make a big difference anyway.

tab = @inferred freqtable(cy)
@test tab == [100, 100, 100, 100]
@test names(tab) == [["A", "B", "C", "D"]]
@test names(tab, 1) isa Array{eltype(cy)}
@test_broken names(tab, 1) isa typeof(cy)
tab = @inferred freqtable(cx, cy)
@test tab == [30 20 20 30;
30 20 20 30;
20 30 30 20;
20 30 30 20]
@test names(tab) == [["a", "b", "c", "d"], ["A", "B", "C", "D"]]
@test names(tab, 1) isa Array{eltype(cx)}
@test names(tab, 2) isa Array{eltype(cy)}
@test_broken names(tab, 1) isa typeof(cx)
@test_broken names(tab, 2) isa typeof(cy)
tab2 = @inferred freqtable(cx, y)
@test tab2 == tab
@test names(tab2) == names(tab)
@test names(tab, 1) isa Array{eltype(cx)}
@test_broken names(tab2, 1) isa typeof(cx)
@test names(tab2, 2) isa typeof(y)
tab2 = @inferred freqtable(x, cy)
@test tab2 == tab
@test names(tab2) == names(tab)
@test names(tab2, 1) isa typeof(x)
@test names(tab, 2) isa Array{eltype(cy)}
@test_broken names(tab2, 2) isa typeof(cy)

tab = @inferred freqtable(cx, cy,
subset=1:20,
Expand All @@ -95,6 +119,10 @@ tab = @inferred freqtable(cx, cy,
0.0 0.0 3.0 2.0
0.0 0.0 1.5 1.0]
@test names(tab) == [["a", "b", "c", "d"], ["A", "B", "C", "D"]]
@test names(tab, 1) isa Array{eltype(cx)}
@test names(tab, 2) isa Array{eltype(cy)}
@test_broken names(tab, 1) isa typeof(cx)
@test_broken names(tab, 2) isa typeof(cy)


const ≅ = isequal
Expand All @@ -110,10 +138,16 @@ tab = @inferred freqtable(mx)
tabc = @inferred freqtable(mcx)
@test tab == tabc == [99, 100, 100, 100, 1]
@test names(tab) ≅ names(tabc) ≅ [["a", "b", "c", "d", missing]]
@test names(tab, 1) isa typeof(mx)
@test names(tabc, 1) isa Array{eltype(mcx)}
@test_broken names(tabc, 1) isa typeof(mcx)
tab = @inferred freqtable(my)
tabc = @inferred freqtable(mcy)
@test tab == tabc == [100, 99, 99, 98, 4]
@test names(tab) ≅ names(tabc) ≅ [["A", "B", "C", "D", missing]]
@test names(tab, 1) isa typeof(my)
@test names(tabc, 1) isa Array{eltype(mcy)}
@test_broken names(tabc, 1) isa typeof(mcy)
tab = @inferred freqtable(mx, my)
tabc = @inferred freqtable(mcx, mcy)
@test tab == tabc == [30 20 20 29 0;
Expand All @@ -123,22 +157,65 @@ tabc = @inferred freqtable(mcx, mcy)
0 0 0 0 1]
@test names(tab) ≅ names(tabc) ≅ [["a", "b", "c", "d", missing],
["A", "B", "C", "D", missing]]
@test names(tabc, 1) isa Array{eltype(mcx)}
@test names(tabc, 2) isa Array{eltype(mcy)}
@test_broken names(tabc, 1) isa typeof(mcx)
@test_broken names(tabc, 2) isa typeof(mcy)
tab = @inferred freqtable(mx, my)
tab2 = @inferred freqtable(mcx, my)
@test tab2 == tab
@test names(tab2) ≅ names(tab)
@test names(tab2, 1) isa Array{eltype(mcx)}
@test_broken names(tab2, 1) isa typeof(mcx)
@test names(tab2, 2) isa typeof(my)
tab2 = @inferred freqtable(mx, mcy)
@test tab2 == tab
@test names(tab2) ≅ names(tab)
@test names(tab2, 1) isa typeof(mx)
@test names(tab2, 2) isa Array{eltype(mcy)}
@test_broken names(tab2, 2) isa typeof(mcy)


tab = @inferred freqtable(mx, skipmissing=true)
tabc = @inferred freqtable(mcx, skipmissing=true)
@test tab == tabc == [99, 100, 100, 100]
@test names(tab) ≅ names(tabc) ≅ [["a", "b", "c", "d"]]
@test names(tab, 1) isa typeof(mx)
@test names(tabc, 1) isa Array{eltype(mcx)}
@test_broken names(tabc, 1) isa typeof(mcx)
tab = @inferred freqtable(my, skipmissing=true)
tabc = @inferred freqtable(mcy, skipmissing=true)
@test names(tab) ≅ names(tabc) ≅ [["A", "B", "C", "D"]]
@test names(tab, 1) isa typeof(my)
@test names(tabc, 1) isa Array{eltype(mcy)}
@test_broken names(tabc, 1) isa typeof(mcy)
@test tab == tabc == [100, 99, 99, 98]
tab = @inferred freqtable(mx, my, skipmissing=true)
tabc = @inferred freqtable(mcx, mcy, skipmissing=true)
@test tab == tabc == [30 20 20 29;
30 20 20 29;
20 30 30 20;
20 29 29 20]
@test names(tab) == names(tabc) == [["a", "b", "c", "d"],
["A", "B", "C", "D"]]
@test names(tab, 1) isa typeof(mx)
@test names(tab, 2) isa typeof(my)
@test names(tabc, 1) isa Array{eltype(mcx)}
@test names(tabc, 2) isa Array{eltype(mcy)}
@test_broken names(tabc, 1) isa typeof(mcx)
@test_broken names(tabc, 2) isa typeof(mcy)
tab2 = @inferred freqtable(mcx, my, skipmissing=true)
@test tab2 == tab
@test names(tab2) ≅ names(tab)
@test names(tab2, 1) isa Array{eltype(mcx)}
@test_broken names(tab2, 1) isa typeof(mcx)
@test names(tab2, 2) isa typeof(my)
tab2 = @inferred freqtable(mx, mcy, skipmissing=true)
@test tab2 == tab
@test names(tab2) ≅ names(tab)
@test names(tab2, 1) isa typeof(mx)
@test names(tab2, 2) isa Array{eltype(mcy)}
@test_broken names(tab2, 2) isa typeof(mcy)


using DataFrames
Expand Down