# Adding a Unitful unit to a vector with missings is slow

**URL:** <https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085>\
**Category:** General Usage\
**Created:** [February 22, 2019, 4:45pm UTC](https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085 "2019-02-22T16:45:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 22, 2019, 4:45pm UTC](https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085/1 "2019-02-22T16:45:16Z")

</div>

I have a vector with a million floats, and some missing values. I want to say that it’s a vector of meters. On Julia 1.0.3:

```julia
julia> using BenchmarkTools, Unitful

julia> v = vcat(fill(1.0, 500000), missing, fill(1.0, 500000));

julia> import Base: *

julia> *(::Missing, ::Unitful.Units) = missing
* (generic function with 400 methods)

julia> @btime v .* u"m";
  60.390 ms (999502 allocations: 31.46 MiB)

julia> @btime copy(v); # for comparison
  1.892 ms (2 allocations: 8.58 MiB)

```

On Julia 1.1 it should be faster, thanks to efforts by @nalimilan. However, what I would really like is to avoid the copy altogether. This should be a 5 ns operation. I thought of `reinterpret`, but unfortunately:

```julia
julia> reinterpret(Union{typeof(1.0u"m"), Missing}, v)
ERROR: ArgumentError: cannot reinterpret `Union{Missing, Float64}` `Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}`, type `Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}` is not a bits type
Stacktrace:
 [1] (::getfield(Base, Symbol("#throwbits#184")))(::Type{Union{Missing, Float64}}, ::Type{Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}}, ::Type{Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}}) at ./reinterpretarray.jl:16
 [2] reinterpret(::Type{Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}}, ::Array{Union{Missing, Float64},1}) at ./reinterpretarray.jl:33
 [3] top-level scope at none:0

```

Did I miss anything?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 22, 2019, 5:18pm UTC](https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085/2 "2019-02-22T17:18:47Z")

</div>

I guess that’s [https://github.com/JuliaLang/julia/issues/26681](https://github.com/JuliaLang/julia/issues/26681)…

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 22, 2019, 7:15pm UTC](https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085/3 "2019-02-22T19:15:51Z")

</div>

I don’t understand what you’re trying to do nor how `missing` is involved. Also `v .* u"m"` throws an error here because `*` isn’t defined.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 22, 2019, 7:36pm UTC](https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085/4 "2019-02-22T19:36:36Z")

</div>

Ah, Unitful#208 hasn’t been merged yet. You just need `*(::Missing, ::Unitful.Units) = missing` to run the code.

Suppose you load a dataframe containing distances, in meters, along with missing values. Feather files can’t contain units, so you need to add the units after loading, hence `df[:distance] = df[:distance] .* u"m"`.

If there’s no missing in the vector, `reinterpret` is glorious

```julia
julia> @btime reinterpret(typeof(1.0u"m"), v);
  51.708 ns (1 allocation: 32 bytes)

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 22, 2019, 8:25pm UTC](https://discourse.julialang.org/t/adding-a-unitful-unit-to-a-vector-with-missings-is-slow/21085/5 "2019-02-22T20:25:57Z")

</div>

OK. Then I’m not sure there’s a good solution. The issue you refer to is about `convert`, but `broadcast` and `.*` will always allocate a new vector AFAIK (changing this would break code). Technically it should be possible to get `reinterpret` to work for `Vector{Union{T,Missing}}` when `T` is a bits type, but there may be reasons not to support it. Maybe file a new issue.
