Skip to content

Allow reinterpret(Union{Missing, T}, ::Vector{Union{Missing, U}}) #31152

@cstjean

Description

@cstjean

This works:

julia> v = [1,2,3,4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> reinterpret(Some{Int64}, v)
4-element reinterpret(Some{Int64}, ::Array{Int64,1}):
 1
 2
 3
 4

It would be very nice if this could be supported, too:

julia> v2 = [1,2,3,4,missing]
5-element Array{Union{Missing, Int64},1}:
 1       
 2       
 3       
 4       
  missing

julia> reinterpret(Union{Missing, Some{Int64}}, v)
ERROR: ArgumentError: cannot reinterpret `Int64` `Union{Missing, Some{Int64}}`, type `Union{Missing, Some{Int64}}` is not a bits type
Stacktrace:
 [1] (::getfield(Base, Symbol("#throwbits#200")))(::Type{Int64}, ::Type{Union{Missing, Some{Int64}}}, ::Type{Union{Missing, Some{Int64}}}) at ./reinterpretarray.jl:16
 [2] reinterpret(::Type{Union{Missing, Some{Int64}}}, ::Array{Int64,1}) at ./reinterpretarray.jl:33
 [3] top-level scope at none:0

I've explained the use case on discourse. Briefly, when working with Unitful vectors, one often has to take out the units (eg. to make a linear regression), then tack them back on the result. Both operations could conceivably be done allocation-free with reinterpret, but are very slow on vectors that contain missings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    missing dataBase.missing and related functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions