How to index a `CatagoricalArray` then make a new array with the same levels


I have been looking at the source code for CategoricalArray but I can’t figure this out.

vec = CategoricalArray([1,2,1,2])
t = vec[1]
# We know what levels 
t.pool # CategoricalArrays.CategoricalPool{Int64,UInt32}([1,2])
newvec = CategoricalArray(fill(t, 5))
newvec.pool # CategoricalArrays.CategoricalPool{Int64,UInt32}([1])
# not [1, 2]

Because newvec.pool is immutable, you can’t alter it after you create the vector.

Anyone have good enough knowledge of the constructors for CategoricalArrays to help me with this?


You can just do levels!(newvec, levels(vec)).


Thanks! I’ll add this to my code.


Just got around to doing this, sorry.

I’m sorry i wasn’t clear earlier. I needed an array where all you are given is a CategoricalValue, not the full array. The solution i ended up using was

levels!(vec, CategoricalArrays.index(CategoricalArrays.pool(x)))


OK. Rather do levels!(vec, levels(CategoricalArrays.pool(x))). Using index instead won’t use the correct ordering of levels in some cases (the ordering of the index is an implementation detail).