# 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:** 7

<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, 8:02pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/123 "2023-11-28T20:02:54Z")

</div>

Still, I don’t think `skip(mean)(x)` is any more convenient than `mean(skipmissing(x))`, and when you add a keyword argument to `skip` it gets pretty ugly:

```julia
skip(cor; use=:pairwise_complete_obs)(X)

```

(R has 4 skip-missing options for `cor`, in addition to the default of just propagating `NA`.)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 28, 2023, 8:08pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/124 "2023-11-28T20:08:50Z")

</div>

`skip()` is shorter than `na.rm=T`, so I think we at least get to R parity with this change. My assumption is that `skip(cor)(x, y)` would always use complete observations of `x` and `y` jointly. I think a lot of the benefit stems from multi-argument functions, where `skipmissings` requires splatting and collecting.

---

<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, 8:13pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/125 "2023-11-28T20:13:37Z")

</div>

> [@CameronBieganek](#):
>
> > [@mbauman](#):
> >
> > The really nice affordance that @mkitti is suggesting is that you can write `using SkipMissings: mean` at the very beginning and then it will _shadow_ `mean` for the code you write.
> 
> I’m not convinced that’s a good idea. It goes against the spirit of generic programming in Julia. The `mean` and `smean` functions are fundamentally different functions.

It’s not really related to generic programming. These are different functions in different modules – that the functions happen to have the same name is fine.

---

<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, 8:16pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/126 "2023-11-28T20:16:22Z")

</div>

I understand that it works, but it is highly misleading to someone reading the code when they see `mean(x)`. That’s why I said it goes against the _spirit_ of generic programming in Julia.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 28, 2023, 8:20pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/127 "2023-11-28T20:20:16Z")

</div>

> [@CameronBieganek](#):
>
> highly misleading

agreed, I’d probably prefer something like:

```julia

let mean = sm.mean, sum=sm.sum
   ...tens or hundreds of lines of code goes here...
end

```

Of course, you could also do:

```julia
@sm begin
... tens or hundreds of lines of code goes here
end

```

and let the sm macro set things up for you

---

<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:** [November 28, 2023, 8:23pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/128 "2023-11-28T20:23:46Z")

</div>

I also think that macros are the best solution. @pdeffebach Don’t you think that better macros in DataFramesMeta would greatly alleviate the pain of working with missing values? Ideally one could add at the top of a block `@passmissing`, `@skipmissing` or a new macro which would combine both behaviors, and everything would work.

---

<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, 8:24pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/129 "2023-11-28T20:24:26Z")

</div>

> [@pdeffebach](#):
>
> My assumption is that `skip(cor)(x, y)` would always use complete observations of `x` and `y` jointly.

What about `skip(cor)(X)`, where `X` is a matrix like this?

```julia
3×3 Matrix{Union{Missing, Int64}}:
 1 2 missing
 3 missing 4
  missing 5 6

```

Then you have to decide if you want to use complete observations or pairwise-complete observations.

---

<div class="post-metadata">

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

</div>

I agree in the sense that `@passmissing`, or just `@pass` and `@skipmissing`, or just `@skip` would be useful. But these would be thin wrappers around `passmissing` etc. rather than a general find-and-replace macro people are suggesting.

This is very easy to do in DataFramesMeta because DataFrames transformations have a single function as their entry point.

I _don’t_ think that a `@sm begin ... end` macro is feasible. Clearly we don’t want `passmissing` to wrap every single function in a block, or to use `skipmissing` for everything. Outside of a few functions like `mean` and `cor`, it seems not easy for a macro to determine what those functions should be.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 28, 2023, 8:30pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/131 "2023-11-28T20:30:23Z")

</div>

> [@pdeffebach](#):
>
> it seems not easy for a macro to determine what those functions should be.

have it be an explicit argument to the macro which is optional…

```julia
@sm [:mean,:cor] begin
...

end

## or, get the default behavior which would be documented

@sm begin

...

end 

```

The biggest issue is the issue with complete observations etc

---

<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:** [November 28, 2023, 8:32pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/132 "2023-11-28T20:32:45Z")

</div>

> [@pdeffebach](#):
>
> I _don’t_ think that a `@sm begin ... end` macro is feasible. Clearly we don’t want `passmissing` to wrap every single function in a block, or to use `skipmissing` for everything. Outside of a few functions like `mean` and `cor`, it seems not easy for a macro to determine what those functions should be.

Yet it seems that [`spreadmissings`](https://github.com/JuliaData/Missings.jl/pull/122) would cover a lot of use cases, right? We could have `@spreadmissings` which would wrap all functions applied to data frame columns in `spreadmissings`. Of course there are exceptions like `quantile(col, [0.1, 0.9])` but these are relatively rare.

---

<div class="post-metadata">

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

</div>

Yes. I need to finish the PR. Hopefully that will make things a lot easier.

---

<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, 8:38pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/134 "2023-11-28T20:38:36Z")

</div>

> [@nalimilan](#):
>
> but these are relatively rare.

I think it would really be a shame to converge on a “solution” that does not include making `quantile` nice. the main motivating functions for me are moments, _ **quantiles** _, and cov/cor

---

<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:** [November 28, 2023, 8:41pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/135 "2023-11-28T20:41:21Z")

</div>

Quantiles are super special as it’s one of the rare statistical functions which takes a vector of data _and_ a vector of quantile positions. But we could work around this using various solutions, including passing a tuple instead of a vector of positions so it’s not too much of a problem.

---

<div class="post-metadata">

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

</div>

Very easy. With the current implementation of `spreadmissings` We would just need to have `Ref` or some sort of other wrapper to protect the `[.1, .9]`.

---

<div class="post-metadata">

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

</div>

For what it’s worth (probably not much), I also feel the pain shared by several others of having to use `skipmissings` to the point of avoiding Julia for data work largely because of that. I also have the impression that the place to address this is at the level of `DataFramesMeta` or whatever other package people (may) use to actually manipulate their data (`Query`, etc), as long as this is technically feasible.

---

<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:07pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/138 "2023-11-28T21:07:04Z")

</div>

> [@pdeffebach](#):
>
> I think `import SM: mean` and then seeing, 100 lines down
> 
> ```julia
> mean(x)
> 
> ```
> 
> is definitely code-smell.

I agree, which is why I would personally tend to stick with the following.

```julia
using SM: SM

# ...

SM.mean(x)

```

Earlier I showed how one might use a module to contain the modified definiton close to the call site, and I would still highly recommend that. For I’m emphasis I will do it again in this context.

```julia
module MySkipMissing
    using Missings.SkipMissing: mean
    function mean_squared(x)
        mean(x)^2
    end
end

```

At the end of the day though, I would still be willing to accommodate the behavior even if I do not agree with it. For quick experiments, I could see why someone might want to replace the aggregation methods quickly.

In the larger scope, what I want to demonstrate here is how flexible Julia is and the range of preferences that Julia can accommodate in a modular fashion.

---

<div class="post-metadata">

**Author:** ![skypuppy](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@skypuppy](https://discourse.julialang.org/u/skypuppy)\
**Post date:** [November 28, 2023, 9:15pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/139 "2023-11-28T21:15:40Z")

</div>

This will be an unpopular solution, but as someone who has designed and worked many databases over the years, I propose that a method of incorporating a “standard” -missing- value, while at the same time, allowing each designer/user to establish their own substitution on a per program or per function basis. Anyone not using the “standard” is responsible for how it is handled in their code. Further, also allow a Julia-wide version of -missing- that is a parameter that can be defined once in their environment and is set from thereon in their future. That person is responsible for propagating that value in any code or function where it is used. Their own “environment variable” as it were.

---

<div class="post-metadata">

**Author:** ![Joseph\_Bradley](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joseph\_Bradley](https://discourse.julialang.org/u/Joseph_Bradley)\
**Post date:** [November 28, 2023, 9:33pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/140 "2023-11-28T21:33:17Z")

</div>

> [@alfaromartino](#):
>
> However, missing values only arise when you work in data analysis, and you always know whether there are missing values in your dataset.

I strongly disagree with the idea that you always know whether there are missings in your data.

Furthermore, it’s good data science to document how you deal with missing data careful. It’s so annoying reading papers that clearly have some strategy to handle missing data that don’t make it explicit - you are then stuck guessing said strategy whenever you want to reproduce something.

---

<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, 9:46pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/141 "2023-11-28T21:46:01Z")

</div>

> [@mkitti](#):
>
> Earlier I showed how one might use a module to contain the modified definiton close to the call site, and I would still highly recommend that. For emphasis I will do it again in this context.

This,

```julia
module MySkipMissing
    using Missings.SkipMissing: mean
    function mean_squared(x)
        mean(x)^2
    end
end

MySkipMissing.mean_squared([1, missing, 2])

```

seems needlessly verbose and complex compared to this:

```julia
using Missings

function mean_squared(x)
    smean(x)^2
end

mean_squared([1, missing, 2])

```

But ultimately we’re just bikeshedding now. 🙂

---

<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, 9:46pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/142 "2023-11-28T21:46:29Z")

</div>

I don’t think that adding macros or shorten names to function would solve the problem. In fact, I have a file that I load that implements some of the suggestions.

From all the options, the only one that makes sense to me is adding a new type. The reasons:

1. My concern is not about `missing`. Sometimes, I need to be careful about the treatment of missing. Rather, my concern is that `missing` isn’t ideal for some type of work, in which case they hinder the writing and reading of the code.  
Ideally, sometimes you want to say: "these types of missings in particular are uniformative, so I don’t want to keep indicating this when I write `x.>y`, take the `mean`, `cor`, or whatever.

This also leads to clean code, because you tell the reader “thee missing in this scenario should be ignored, and I’m certain that you can do it for all the code”.

1. Other options are troublesome, in particular for new users. When you start to add `smean` or short names like that, then someone who reads code ends up thinking “what smean does?”. The option of directly shadowing `mean` I consider it even more problematic, because you don’t know what type of mean is used, unless you search for whether a corresponding package was loaded.

In this sense is that I would’ve preferred to have ignore missing as the default, while still having both behaviors as options. This means `isequal` exchanging roles with `==`, or have `keepmissing` instead of `skipmissing`.

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.

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

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