From da9283d5e5703772cb7cf39ecdea2280746af968 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Thu, 7 May 2020 11:18:20 +0200 Subject: [PATCH] Always return CategoricalArray names when input is CategoricalArray Previously this was the case only when mixing a `CategoricalArray` with another column, as the specialized `CategoricalArray` method used `levels`. --- src/freqtable.jl | 3 ++- test/freqtable.jl | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/freqtable.jl b/src/freqtable.jl index f7e5632..f3e7fbd 100644 --- a/src/freqtable.jl +++ b/src/freqtable.jl @@ -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) diff --git a/test/freqtable.jl b/test/freqtable.jl index 0a39810..13367a6 100644 --- a/test/freqtable.jl +++ b/test/freqtable.jl @@ -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)