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

<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 29, 2023, 12:28am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/163 "2023-11-29T00:28:59Z")

</div>

> [@simsurace](#):
>
> You can simply require that the user choose one or the other

This just doesn’t work when you want to use multiple packages and each package maintainer had a different preference right?

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [November 29, 2023, 12:30am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/164 "2023-11-29T00:30:13Z")

</div>

> [@Benny](#):
>
> Is it me or does this not do anything like `skipmissing`?

I think what @alfaromartino wants here are predicates for filtering that also remove the missings, to enable things like `x[ishigher.(x, b)]` or `filter(a -> ishigher(a, b), x)`. That way you no longer need to nest with `skipmissing`, as you would today with `skipmissing(x[x .> b])` or `filter(>(b), skipmissing(x))`

---

<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:32am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/165 "2023-11-29T00:32:52Z")

</div>

That’s the thing, those scalar operations don’t skip missings because they propagate one argument’s missings and can’t remove elements like `skipmissing` does, which will matter for operations that depend on the collection’s length e.g. replacing `missing`s with `false`s instead of skipping will affect `mean`. It seems like disparate actions are being muddled here just because they all hande `missing`s.

---

<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 29, 2023, 12:37am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/166 "2023-11-29T00:37:17Z")

</div>

Can anyone point to a publicly available (maybe govt produced) moderate size dataset with missing values and some suggested analyses that could be done with them so we can have a kind of “playground”?

I can probably think of some with some research but maybe someone has a really good example along the lines of a manageable version of @pdeffebach’s comment:

> [@pdeffebach](#):
>
> I want to emphasize that the surveys I work with have 1000+ variables and we create maybe a hundred or more new variables derived from those.

Maybe not 1000+ variables, but something with a few tens of thousands of rows and a hundred ish columns and liberal use of missing?

---

<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 29, 2023, 12:41am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/167 "2023-11-29T00:41:06Z")

</div>

```julia
julia> using DataFrames

julia> using ShiftedArrays: lag

julia> df = DataFrame(rand(100, 2), :auto);

julia> df.x1_lag1 = copy(lag(df.x1));

julia> df.x2_lag2 = copy(lag(df.x2, 2));

```

now compute a `cor` of `x1_lag1` and `x2_lag2` without pulling your hair out

---

<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 29, 2023, 12:45am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/168 "2023-11-29T00:45:54Z")

</div>

and for a subset of lags restricted to positive values!

---

<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:52am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/169 "2023-11-29T00:52:35Z")

</div>

> [@adienes](#):
>
> now compute a `cor` of `x1_lag1` and `x2_lag2` without pulling your hair out

Shouldn’t you provide a different `default` value for `lag` then?

---

<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 29, 2023, 1:03am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/170 "2023-11-29T01:03:09Z")

</div>

no?

sorry if that’s curt, but I just don’t really see how that helps. a default value would not be appropriate

---

<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, 1:12am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/171 "2023-11-29T01:12:16Z")

</div>

Yeah it’s just not clear to me what the desired outcome is from manually introducing different numbers of `missing`s into different columns and performing an operation over them.

---

<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 29, 2023, 1:15am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/172 "2023-11-29T01:15:04Z")

</div>

to get a correlation of one feature lagged to another

this is super-duper-giga-extremely common in building signals on financial data

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [November 29, 2023, 1:18am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/173 "2023-11-29T01:18:29Z")

</div>

> [@adienes](#):
>
> now compute a `cor` of `x1_lag1` and `x2_lag2` without pulling your hair out

I’m not a data scientist, but is there more to it than this?

```julia-repl
julia> nomissingdf = dropmissing(df);

julia> using Statistics

julia> cor(nomissingdf.x1_lag1, nomissingdf.x2_lag2)
0.13004606550437073

```

For other filtering steps (e.g., positive values only), apply them to `nomissingdf` as you would otherwise?

---

<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, 1:19am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/174 "2023-11-29T01:19:47Z")

</div>

I’m aware of what correlations are, but I definitely don’t work with financial data. I just don’t understand why you’re introducing `missing`s into it. Is there no standard practice for shifted data, like zero-padding or excluding non-overlapping ends?

---

<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 29, 2023, 1:20am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/175 "2023-11-29T01:20:11Z")

</div>

now imagine you have a bunch more features and a bunch more lags, you do a diagonal join, etc. etc.  
if you `dropmissing(df)` you will have 0 rows. `cor` should only drop the rows used for that calculation, not every single row in the entire table that contains any missing

@Benny this basically is “excluding non-overlapping ends,” I just don’t want to do that by hand over and over and over

---

<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 29, 2023, 1:22am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/176 "2023-11-29T01:22:04Z")

</div>

Just in case, the matter is not whether you can perform operations like this. You can. In fact, you could simply do:

```julia
df2 = dropmissing(df, [:x1_lag1, :x2_lag2], view=true)

cor(df2.x1_lag1, df2.x2_lag2)

```

Note that writing `dropmissing(df)` drops more missing than what you need. And if you also want to modify the original df, you need to add the `view = true`.

But every two lines of code you write, you have to think about missings for some types of work where missings are uninformative. It gets extremely tiring after a few hours.

Julia has always been about making the code as simple as possible, indicating how you caa implement in 10 lines what other languages require 100 lines of code. So, this is the case where the opposite happens, where some code that should have 500 lines ends up being 1000 lines full of `skipmissing`, `passmissing`, `dropmissing`, own defined functions of `ishigher` rather than `.>`, etc.

But, again, I’m not sure there’s an easy solution.

---

<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 29, 2023, 1:25am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/177 "2023-11-29T01:25:01Z")

</div>

> [@alfaromartino](#):
>
> It gets extremely tiring after a few hours.

I think this is what is hard to convey. I can understand why some people might feel unsympathetic when there are so many ~clear and easy~ solutions to this problem. but after the 1000th time the language says “neener neener, didja think about missing???” and having to reply “yes, I promise, it’s ok” with 11 unnecessary characters, it gets exhausting

---

<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 29, 2023, 1:33am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/178 "2023-11-29T01:33:57Z")

</div>

> [@adienes](#):
>
> I just don’t want to do that by hand over and over and over

I mean, you have a specific use case where you are going to repeatedly do a similar thing over and over handling missing in a way that’s consistent for you… So write a function that does that?

`smcor()` or something

This doesn’t seem different to me than having a particular kind of plot you want to do over and over and writing a particular plotting function to do it.

---

<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 29, 2023, 1:36am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/179 "2023-11-29T01:36:56Z")

</div>

right, and now we’re back to “if you like `polars` 's missing semantics so much why don’t you just go rewrite your own statistical libraries”

I’m not saying you’re entirely wrong, but this is definitely not a particularly user-friendly response to a touring data scientist who just wants to get some work done

---

<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 29, 2023, 1:40am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/180 "2023-11-29T01:40:32Z")

</div>

Anyone who wants `scor` in Missings.jl should chime in here:

> <https://github.com/JuliaData/Missings.jl/issues/146>
>
> xref: https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-de…fault/106756
> 
> There are some users who dislike \`mean(skipmissing(x))\` because they view it as too verbose or too many keystrokes. Aggregation functions that skip missing values are very commonly needed, so I think it would be worth adding convenience skip-missing methods for Base aggregation functions to Missings.jl, e.g.
> 
> \- \`smean\`
> \- \`ssum\`
> \- \`svar\`
> \- \`scor\`
> 
> The \`svar\` and \`scor\` functions could have a keyword argument to control exactly how missing values are skipped, similar to \[\`cor\` in R\](https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/cor).

---

<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, 1:42am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/181 "2023-11-29T01:42:45Z")

</div>

> [@alfaromartino](#):
>
> But, again, I’m not sure there’s an easy solution.

I really doubt it, too. Sure it’s annoying to assemble building blocks so often, and it’s also annoying to make custom functions to do the repeated patterns quicker. But it doesn’t make sense to distribute many versions of the same thing with different defaults and processing.

About the `cor` example, it makes sense to drop the non-overlapping `missing` ends to compute a correlation. It could also make sense in some contexts to make the defaults zeros in the first place. If the data had a bunch of `missing` in the _middle_, it could make sense to not drop those and propagate the `missing` to indicate that the data is too poorly recorded to be useful. Do they all deserve their own version of `cor` or their own version of `Statistics`? We can only introduce so many new things before it becomes bloat, and even then it won’t cover much.

---

<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 29, 2023, 1:46am UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/182 "2023-11-29T01:46:10Z")

</div>

> [@Benny](#):
>
> doesn’t make sense to distribute many versions of the same thing with different defaults and processing. […] Do they all deserve their own version of `cor` or their own version of `Statistics`?

Ironically, I think this is one place where all the code sharing in Julia is detrimental. `numpy.mean` and `polars.mean` have different behaviors and each makes total sense within their contexts

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

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