Skip to content

Commit

Permalink
Always return CategoricalArray names when input is CategoricalArray
Browse files Browse the repository at this point in the history
Previously this was the case only when mixing a `CategoricalArray` with another column,
as the specialized `CategoricalArray` method used `levels`.
  • Loading branch information
nalimilan committed May 7, 2020
1 parent 632cd98 commit da9283d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ function _freqtable(x::NTuple{n, AbstractCategoricalVector}, skipmissing::Bool =
len = map(length, x)
allowsmissing = map(v -> eltype(v) >: Missing, x)
lev = map(x) do v
eltype(v) >: Missing && !skipmissing ? [levels(v); missing] : allowmissing(levels(v))
l = eltype(v) >: Missing && !skipmissing ? [levels(v); missing] : levels(v)
CategoricalArray{eltype(v)}(l)
end
dims = map(length, lev)

Expand Down
3 changes: 3 additions & 0 deletions test/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ for docat in [false, true]
0 5
1 6]
@test names(tab) == [["Iris-setosa", "Iris-versicolor", "Iris-virginica"], [false, true]]
@test (names(tab, 2) isa CategoricalArray) == docat

tab = freqtable(iris, :Species, :LongSepal, subset=iris.SepalWidth .< 3.8)
@test tab == [2 0
0 5
1 6]
@test names(tab[1:2, :]) == [["Iris-setosa", "Iris-versicolor"], [false, true]]
@test (names(tab, 2) isa CategoricalArray) == docat
iris_nt = (Species = iris.Species, LongSepal = iris.LongSepal)
@test freqtable(iris, :Species, :LongSepal) == freqtable(iris_nt, :Species, :LongSepal)

Expand Down

0 comments on commit da9283d

Please sign in to comment.