Skip to content
New issue

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

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

Already on GitHub? # to your account

Implement skipmissing argument to levels #391

Merged
merged 3 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -753,14 +753,29 @@ end
leveltype(::Type{T}) where {T <: CategoricalArray} = leveltype(nonmissingtype(eltype(T)))

"""
levels(x::CategoricalArray)
levels(x::CategoricalArray; skipmissing=true)
levels(x::CategoricalValue)

Return the levels of categorical array or value `x`.
This may include levels which do not actually appear in the data
(see [`droplevels!`](@ref)).
`missing` will be included only if it appears in the data and
`skipmissing=false` is passed.

The returned vector is an internal field of `x` which must not be mutated
as doing so would corrupt it.
"""
DataAPI.levels(A::CategoricalArray) = levels(A.pool)
@inline function DataAPI.levels(A::CatArrOrSub{T}; skipmissing::Bool=true) where T
if eltype(A) >: Missing && !skipmissing
if any(==(0), refs(A))
T[levels(pool(A)); missing]
else
convert(Vector{T}, levels(pool(A)))
end
else
levels(pool(A))
end
end

"""
levels!(A::CategoricalArray, newlevels::Vector; allowmissing::Bool=false)
Expand Down
1 change: 0 additions & 1 deletion src/subarray.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# delegate methods for SubArrays to support view

DataAPI.levels(sa::SubArray{T,N,P}) where {T,N,P<:CategoricalArray} = levels(parent(sa))
isordered(sa::SubArray{T,N,P}) where {T,N,P<:CategoricalArray} = isordered(parent(sa))
# This method cannot support allowmissing=true since that would modify the parent
levels!(sa::SubArray{T,N,P}, newlevels::Vector) where {T,N,P<:CategoricalArray} =
Expand Down
28 changes: 28 additions & 0 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2260,4 +2260,32 @@ end
Vector{CategoricalVector{<:Any, <:Integer, <:Any, <:Any, Union{}}}
end

@testset "levels with skipmissing argument" begin
for x in (categorical(["a", "b", "a"], levels=["b", "c", "a"]),
view(categorical(["c", "b", "a"], levels=["b", "c", "a"]), 2:3))
@test @inferred(levels(x)) == ["b", "c", "a"]
@test @inferred(levels(x, skipmissing=true)) == ["b", "c", "a"]
@test @inferred(levels(x, skipmissing=false)) == ["b", "c", "a"]
end

for x in (categorical(Union{String, Missing}["a", "b", "a"], levels=["b", "c", "a"]),
view(categorical(Union{String, Missing}["c", "b", "a"], levels=["b", "c", "a"]), 2:3),
view(categorical(Union{String, Missing}[missing, "b", "a"], levels=["b", "c", "a"]), 2:3))
@test @inferred(levels(x)) == ["b", "c", "a"]
@test levels(x, skipmissing=true) == ["b", "c", "a"]
@test levels(x, skipmissing=true) isa Vector{String}
@test levels(x, skipmissing=false) == ["b", "c", "a"]
@test levels(x, skipmissing=false) isa Vector{Union{String, Missing}}
Comment on lines +2275 to +2278
Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't find a way to make this inferrable, even by wrapping this in a function. Not sure why. It's not terrible since the return type is inferred as Union{Vector{Union{String, Missing}}, Vector{String}}. But still...

end

for x in (categorical(Union{String, Missing}["a", "b", missing], levels=["b", "c", "a"]),
view(categorical(Union{String, Missing}["c", "b", missing], levels=["b", "c", "a"]), 2:3))
@test @inferred(levels(x)) == ["b", "c", "a"]
@test levels(x, skipmissing=true) == ["b", "c", "a"]
@test levels(x, skipmissing=true) isa Vector{String}
@test levels(x, skipmissing=false) ≅ ["b", "c", "a", missing]
@test levels(x, skipmissing=false) isa Vector{Union{String, Missing}}
end
end

end