# Why are missing values not ignored by default?

**URL:** <https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756>\
**Category:** Internals & Design\
**Tags:** data, missing-values\
**Created:** [November 26, 2023, 1:45am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756 "2023-11-26T01:45:36Z")\
**Posts on this page:** 20\
**Page:** 8

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 28, 2023, 9:52pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/143 "2023-11-28T21:52:49Z")

</div>

Can you tell me a little bit more about how this new type works? What would you like it to do? Is it basically missing with different default behavior?

Here’s some code to start with along the lines of what I provided above.

> **Setup code for \`MyMissing\` and \`MyMissingDataFrame\`**
>
> ```julia-repl
> julia> using CSV, Statistics, MappedArrays, DataFrames
> 
> julia> struct MyMissing end
> 
> julia> const mymissing = MyMissing()
> MyMissing()
> 
> julia> struct MyMissingDataFrame # <: AbstractDataFrame (work in progress)
> parent::DataFrame
> end
> 
> julia> Base.parent(mmdf::MyMissingDataFrame) = getfield(mmdf, :parent)
> 
> julia> Base.getproperty(mmdf::MyMissingDataFrame, sym::Symbol) =
> mappedarray(x->ismissing(x) ? MyMissing() : x, Base.getproperty(parent(mmdf), sym))
> 
> julia> Base.ismissing(::MyMissing) = true
> 
> julia> Base.nonmissingtype(::Type{T}) where {T >: MyMissing} = Base.typesplit(T, MyMissing)
> 
> ```

```julia-repl
julia> mmdf = MyMissingDataFrame(df)
MyMissingDataFrame(6×2 DataFrame
 Row │ col1 col2    
     │ String3 Int64?  
─────┼──────────────────
   1 │ 5 6
   2 │ 1 2
   3 │ 30 31
   4 │ 22 23
   5 │ NA missing 
   6 │ 50 missing )

julia> mmdf.col2
6-element mappedarray(var"#5#6"(), ::SentinelArrays.SentinelVector{Int64, Int64, Missing, Vector{Int64}}) with eltype Union{MyMissing, Int64}:
  6
  2
 31
 23
   MyMissing()
   MyMissing()

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 28, 2023, 10:17pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/144 "2023-11-28T22:17:04Z")

</div>

> [@alfaromartino](#):
>
> Upon reflection after all the discussion, I think the best solution is adding a new type. This extends the possibilities, without restricting the ones we have.

One of the issues with adding a new missing type is that the current `missing` is an integral part of three-valued logic (3VL) in Julia. Here’s the 3VL truth table for `|`:

```julia
julia> using DataFrames

julia> x = [true, true, true, false, false, false, missing, missing, missing];

julia> y = [true, false, missing, true, false, missing, true, false, missing];

julia> DataFrame(x = x, y = y, x_or_y = x .| y)
9×3 DataFrame
 Row │ x y x_or_y
     │ Bool? Bool? Bool?
─────┼───────────────────────────
   1 │ true true true
   2 │ true false true
   3 │ true missing true
   4 │ false true true
   5 │ false false false
   6 │ false missing missing
   7 │ missing true true
   8 │ missing false missing
   9 │ missing missing missing

```

If we add `newmissing`, that truth table will explode in size—we would now be dealing with four-valued logic (4VL). That sounds like a nightmare to me.

To clarify, the three values referred to in 3VL are three logical values: `true`, `false`, and `unknown`. Julia follows SQL and R and cheats by representing `unknown` as `missing`. (This has some subtle issues, since `unknown == unknown` should return `true`, but `missing == missing` returns `missing`.) A four-valued logic would have a fourth logical value, like `newunknown`. But continuing with the `missing` tradition, a 4VL logical vector would be of type `Union{Boolean, Missing, NewMissing}`.

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [November 28, 2023, 10:17pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/145 "2023-11-28T22:17:57Z")

</div>

For instance, these are some functions I’d apply for the types of missing I have in mind.

![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/8956a11e351a984eefd78aaa2353b29bbb4f2913.png)

Ideally, my code would be more readable if I could directly use expressions like `x .> 0` instead. In addition, functions like sum, mean, cor, etc should also ignore the type of missing.

In any case, the implementation may be so complicated that it’d be too hard to do. I imagine there must be lots of special cases that should be thought of, unintended consequences I’m not considering, breaking composability with other packages, etc.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 28, 2023, 10:19pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/146 "2023-11-28T22:19:19Z")

</div>

Imho, there is not much difference between using a type providing additional methods vs shadowing existing methods. In both cases, the underlying problem is that there are two reasonable default choices of how missing values are to be treated and we need some way to configure code to select one of them:

```julia
# Pick any of these methods to configure your analysis
# 1. Import shadowing methods
using Missings.SkipMissing: mean
# 2. Convert data to new type
df = MyMissingDataFrame(df)
...
# some analysis much later in the file
mean(df.whatever)

```

In any case, functions with the same name are configured to behave differently. More local approaches are also conceivable, i.e., using macros or blocked definitions as syntactic markers:

```julia
let mean = mean ∘ skipmissing
     ...
     mean(df.whatever)
end
# or macro based
@with_missing_skipped begin
    ...
    mean(df.whatever)
end

```

In the end, it’s mainly about defaults and how easily – also in terms of readability – these can be changed.

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [November 28, 2023, 10:20pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/147 "2023-11-28T22:20:53Z")

</div>

yes, when I said we should wait for Julia 2.0, I was trying to convey the idea that a new type of missing is maybe too complex and affects the fundamentals of Julia. So that maybe there’s no solution, explaining why Python and other languages had to made a decision about it, but none of them covers all cases.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 28, 2023, 10:23pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/148 "2023-11-28T22:23:59Z")

</div>

I don’t see what you mean. 3VL is not a concept in Julia. Missing is just some type, we can introduce another type that behaves similarly. Why would we need 4VL instead of just `Union{Bool, NewMissing}`?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 28, 2023, 10:24pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/149 "2023-11-28T22:24:30Z")

</div>

This must be some sort of record: almost 150 posts about something that is not there!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 28, 2023, 10:29pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/150 "2023-11-28T22:29:27Z")

</div>

> [@alfaromartino](#):
>
> For instance, these are some functions I’d apply for the types of missing I have in mind.
> 
> ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/8956a11e351a984eefd78aaa2353b29bbb4f2913.png)
> 
> Ideally, my code would be more readable if I could directly use expressions like `x .> 0` instead. In addition, functions like sum, mean, cor, etc should also ignore the type of missing.
> 
> In any case, the implementation may be so complicated that it’d be too hard to do. I imagine there must be lots of special cases that should be thought of, unintended consequences I’m not considering, breaking composability with other packages, etc.

This is not too hard actually. If I understand your code, the binary operator always returns `false` if a missing is present, right?

Also, I think you could have written these more succintly.

```julia
ishigheq(a, b) = !ismissing(a) && a >= b
ishigher(a, b) = !ismissing(a) && a > b
islower(a, b) = !ismissing(a) && a < b
isloweq(a, b) = !ismissing(a) && a <= b

```

**edit: fixed the inversion, thanks to @Benny**

Anyways, that said, here are the two additional binary operator definitions that we need.

```julia
julia> Base.:<(a::MyMissing, b) = false

julia> Base.:<(a, b::MyMissing) = false

```

Here’s a demo.

```julia
julia> 5 < mymissing
false

julia> mymissing < 5
false

julia> 5 > mymissing
false

julia> 3.0 >= mymissing
false

julia> 5 <= mymissing
false

julia> mmdf.col2 .< 5
6-element BitVector:
 0
 1
 0
 0
 0
 0

julia> mmdf.col2 .> 5
6-element BitVector:
 1
 0
 1
 1
 0
 0

julia> mmdf.col2 .<= 6
6-element BitVector:
 1
 1
 0
 0
 0
 0

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 28, 2023, 10:31pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/151 "2023-11-28T22:31:44Z")

</div>

> [@jar1](#):
>
> I don’t see what you mean. 3VL is not a concept in Julia.

Actually it is. See [this section of the manual](https://docs.julialang.org/en/v1/manual/missing/#Logical-operators). Quoting from that section:

> Logical (or boolean) operators [`|`](https://docs.julialang.org/en/v1/base/math/#Base.:%7C), [`&`](https://docs.julialang.org/en/v1/base/math/#Base.:&) and [`xor`](https://docs.julialang.org/en/v1/base/math/#Base.xor) are another special case since they only propagate `missing` values when it is logically required. For these operators, whether or not the result is uncertain, depends on the particular operation. This follows the well-established rules of [_three-valued logic_](https://en.wikipedia.org/wiki/Three-valued_logic) which are implemented by e.g. `NULL` in SQL and `NA` in R.

In other words, the correct propagation of missing has already been defined for the logical operators `|`, `&`, `xor`, and `!`. Presumably we would also need to expand the truth tables to include `newmissing`, if it was introduced.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 28, 2023, 10:32pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/152 "2023-11-28T22:32:31Z")

</div>

> [@alfaromartino](#):
>
> yes, when I said we should wait for Julia 2.0, I was trying to convey the idea that a new type of missing is maybe too complex and affects the fundamentals of Julia. So that maybe there’s no solution, explaining why Python and other languages had to made a decision about it, but none of them covers all cases.

I’m trying really, really hard to disabuse of you this notion. Julia was built to do exactly this. Fundamental to the design of Julia 1 is the ability to extend operators to new types. We do not need a Julia 2 to do this.

---

<div class="post-metadata">

**Author:** ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)\
**Post date:** [November 28, 2023, 10:43pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/153 "2023-11-28T22:43:42Z")

</div>

> [@CameronBieganek](#):
>
> If we add `newmissing`, that truth table will explode in size—we would now be dealing with four-valued logic (4VL). That sounds like a nightmare to me.

This is not required. You can simply require that the user choose one or the other, and never define any methods for mixing the two. The idea being that the type of missing is injected into the data early on (preferably when reading the file) and never changed.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 28, 2023, 10:46pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/154 "2023-11-28T22:46:58Z")

</div>

I don’t think we need to define `Base.|(::Missing, ::NewMissing)` etc. The answer isn’t obvious and people should specify what they mean when they’re mixing these together.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [November 28, 2023, 11:17pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/155 "2023-11-28T23:17:28Z")

</div>

“The user should decide” is the current behavior, however. I don’t see how this solution improves things.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 28, 2023, 11:25pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/156 "2023-11-28T23:25:45Z")

</div>

I’m saying the user should decide what should happen when combining `Missing` with `NewMissing`. But normally my data won’t have `NewMissing` and @adienes data won’t have `Missing`, it’s usually one or the other – mixing them will be less common.

---

<div class="post-metadata">

**Author:** ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)\
**Post date:** [November 28, 2023, 11:38pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/157 "2023-11-28T23:38:41Z")

</div>

I think it is important not to pile too many features onto any of the proposals to keep them actionable. The main complaint has been that default behavior should be different, so in my view one first-order solution is a package which provides a type that is a simple replacement of `Base.missing` and has all the desired semantics. Basically, do in a package what you would have done in `Base` if you had been in charge before Julia v1. There is nothing special about things defined in `Base`.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 28, 2023, 11:42pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/158 "2023-11-28T23:42:47Z")

</div>

It sounds like one of the main goals of `newmissing` would be to make `mean(x)` and `sum(x)` “just work”. But in order for these functions to “just work”, they would need special handling of `newmissing`. For example, a naive implementation of `mean` might look like this:

```julia
function mean(itr)
    sum = 0
    n = 0
    for x in itr
        if x !== newmissing
            sum += x
            n += 1
        end
    end
    sum/n
end

```

Thus, only some aggregation functions would “just work”. Other aggregation functions that do not have special handling for `newmissing` would not work. This is basically the same situation as defining `smean`, `ssum`, etc, but at least with `smean` we are not defining a new missing data type.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 28, 2023, 11:43pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/159 "2023-11-28T23:43:09Z")

</div>

I’m not so sure a new type will help much. because after all, `CSV.jl` or `lag` will fill empty values with `missing` in a way I cannot control, so I’d have to `coalesce.(input, newmissing)` everywhere anyway

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 28, 2023, 11:44pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/160 "2023-11-28T23:44:20Z")

</div>

All those libraries are gonna have to grow a missingvalue argument.

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [November 28, 2023, 11:47pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/161 "2023-11-28T23:47:38Z")

</div>

that’s exactly one of the features I was thinking. It leads me to think that the implementation of a new type is too complex. It’ll probably break composability with other packages also.

And the other possible solutions look more like patches, not actually reflecting a structural solution.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 29, 2023, 12:11am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/162 "2023-11-29T00:11:16Z")

</div>

> [@alfaromartino](#):
>
> For instance, these are some functions I’d apply for the types of missing I have in mind.
> 
> ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/8956a11e351a984eefd78aaa2353b29bbb4f2913.png)
> 
> Ideally, my code would be more readable if I could directly use expressions like `x .> 0` instead. In addition, functions like sum, mean, cor, etc should also ignore the type of missing.

Is it me or does this not do anything like `skipmissing`? For one, it only skips the comparison to a default `false` if `a` is missing, it’ll propagate missing `b`. For another, it’s a scalar operation, `skipmissing` removes `missing` elements from collections.

Creating data indistinguishable from real data for missing `a` and treating missing `b` normally is too specific, it just doesn’t seem like something that should be handled by a type, macro, or derivative functions with such a blanket setting. This could instead be accomplished by processing a collection of `a` values `(!ismissing).(A)` before doing anything with `b`, perhaps even lazily by some value substitution wrapper.

> [@mkitti](#):
>
> Also, I think you could have written these more succintly.
> 
> ```julia
> ishigheq(a, b) = ismissing(a) && a >= b
> 
> ```

Should be `!ismissing(a)` because `abool == 0` inverts `abool`.

[Previous page](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756.md?page=7)

[Next page](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756.md?page=9)
