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

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

</div>

Right, what should this do?

```julia
a=[1,2,missing,3,4]
b = lag(a)
cor(a,b)

```

---

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

</div>

the behavior of `missing_cor` here [Why are missing values not ignored by default? - #106 by adienes](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/106)

---

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

</div>

> [@adienes](#):
>
> `numpy.mean` and `polars.mean` have different behaviors and each makes total sense within their contexts

They differ because of the data types, not because of how they handle certain values. The latter is much more diverse.

> [@adienes](#):
>
> the behavior of `missing_cor` here

dlakelan was implying that there isn’t just 1 way to handle middle `missing`s. Someone else would write a different function from you.

---

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

</div>

I’ve submitted the following pull request to make it easier to create `Missing`-like types.

> <https://github.com/JuliaLang/julia/pull/52341>
>
> Changes most of Base to use
> \* \`ismissing\` instead of \`=== missing\`
> \* \`!ismissing…\` instead of \`!== missing\`
> 
> I made sure not to touch \`Core.compiler\`.
> 
> This allows new \`Missing\`-like types to utilize infrastructure such as \`skipmissing\` by defining \`ismissing(::MyMissing)\`.

---

<div class="post-metadata">

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

</div>

@adienes _or others who want this behavior_, could you please tell what convenience issues would remain if some package (`Missings`, `MissingStatistics`, whatever) defined all common aggregation functions (Statistics + StatsBase) that skip missings automatically? So that the user just had to type `scor`/`SM.cor`/bikeshed instead of `cor`.

Seems like best of both worlds: missing handling that is both explicit, local, and without noticeable typing overhead.

---

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

</div>

Suppose you’re measuring the intensity of light from physical particles experiments or something, normally it’s a small number determined by thermal noise but every so often there’s a big flash… Sometimes that big flash can induce a saturation and failure to measure and then a missing is emitted. Your two detectors give the following measurements

```julia
a= [1,2,1,3,missing,3,1,4]
b=[2,1,3,1,856459,1,2,1]

```

Missing just means something COMPLETELY different here from the financial or employment survey or whatever context.

---

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

</div>

R’s cor takes the approach of having a couple keywords with a lot of settings. Not only do they handle missing values, a couple specify whether you want to throw _errors_, that it anticipates that some missing value patterns is so horrendous in some use cases that a user would repeatedly type that keyword to catch bad data. Even then, these settings don’t process all patterns of missing values in every possible way so you’ll have to do it separately anyway, which I would usually prefer instead of keyword settings.

---

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

</div>

I can understand and appreciate the value of this type of `missing`

but maybe the real complaint is that `missing` arises too easily? it seems all the arguments for status quo treat it as an “unobserved observation.” but then `CSV.jl` when reading a truncated line will fill the rest with `missing`, which makes an _assumption_ that those values should have been valid observations but were otherwise unobserved

perhaps those should fill to `nothing` ? and same for `lag` and diagonal joins

---

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

</div>

You _could_ replace `missing` with `nothing` i.e. in `lag`’s `default`, I don’t see how it’ll help because we still have to handle the not-actual-data, and `nothing` throws errors instead of propagating, which seems harder to work with.

---

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

</div>

The original blog post that introduced Julia’s `missing` explicitly recommends using `missing` for “data scientist’s null” and `nothing` for “software engineer’s null”: [First-Class Statistical Missing Values Support in Julia 0.7](https://julialang.org/blog/2018/06/missing/#from_nullable_to_missing_and_nothing). I think this is more or less what @adienes is suggesting.

---

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

</div>

This is why I say that the Bayesian viewpoint may give a really different view on how missing should be handled.

In my example I would want to estimate the actual flash intensity, I would likely do that by building a model and in that model the non missing data would enter into the estimate while the missing data would be imputed.

For the lagged example I’d probably impute the missing values by repetition or by generating a random number of a certain type.

---

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

</div>

Yeah, that’s what I mean by errors vs propagation. The labels aren’t very appropriate in this thread because we have data scientists who are very vocal about not wanting to propagate `missing`s, well some of them anyway. Whichever we use, the error vs propagation upon operations aspect isn’t the end-all-be-all, we can evidently handle either in many many ways before and after the operations. I’m not sure replacing missing-handling with nothing-handling with more thrown errors is the easy direction.

---

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

</div>

It’s why I’d really like to see an example real world dataset, because I can’t think of an example where skipmissing is that big of a deal, either the missing are benign and a small fraction of the data, or I want to do imputation or modeling. There just isn’t much in between.

---

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

</div>

> [@dlakelan](#):
>
> and a small fraction of the data,

it only takes one…

---

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

</div>

> [@adienes](#):
>
> to get a correlation of one feature lagged to another
> 
> this is super-duper-giga-extremely common in building signals on financial data

This is a bit of a tangent, but is it common to add lagged columns to your dataframe for computing lagged correlations, and is this a main source of missings in practical data science? Again, I don’t do a lot of data analysis, but my instinct here would be to reach for signal processing functions from `StatsBase`:

```julia-repl
julia> using StatsBase

julia> lags = [1, 3, 10];

julia> crosscor(df.x1, df.x2, lags)
3-element Vector{Float64}:
  0.019144023607046545
 -0.09897572568100832
 -0.0324592679530158

```

I appreciate the desire to reduce boilerplate when dealing with actually missing data from incomplete survey responses, faulty sensors, et cetera, but for the structural missings from lags and joins, it seems like it would be better to avoid them altogether? Or at least use `nothing` instead of `missing` to emphasize their structural nature, and have a concise idiom built into `DataFrames.jl` for skipping over `nothing` in the obvious way in arbitrary multivariate aggregations.

---

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

</div>

> [@adienes](#):
>
> only takes one…

Right, but if it’s a small number often it’s ignorable you can just delete those observations from the dataset and move on, and if it’s a large number it needs modeling… it’s the intermediate case where there’s like more than you’d be happy just dropping and yet maybe you want some quick and dirty results without doing the modeling of proper imputation… I still think there’s probably a simple imputation method that might be worthwhile in most cases. Like for each missing select a random non-missing element from the vector, or linearly interpolate, or some such.

It’s part of why I’m interested in seeing some real world data because I’m wondering if there isn’t a solution that involves having a small set of simple imputation tools which could be applied to the data

When it comes to the structural stuff like the lag example I’m with @danielwe I think using the right tool for the job instead of building it by hand. crosscor, autocor, and related functions, which are also probably way more space efficient and have other advantages compared to doing stuff by hand adding lagged columns to a data frame.

I guess one question is how much of the issues you have with missing comes from assumptions about the way to do things that come from ecosystems where different patterns are common.

I’m not trying to minimize your struggles more like wrap my head around why I haven’t experienced similar struggles?

---

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

</div>

> if it’s a large number it needs modeling

nope! not always

here is another very real example for me. I have said the word “diagonal join” a few times but maybe it’s not clear what I mean by that. in this situation, a table is used to flatten out updates corresponding to the same market order in a financial trading application.

in this dummy example, it may appear that I should just do a `groupby(:order_id)` and combine to get only one row for each with `order_px` and `fill_px`. but in the general case, this is not really appropriate, since

- a single order may have a large number of independent fills
- the relative arrival order of these updates matters quite a lot and almost all the analysis will care about rolling statistics over each update sequentially

However, I still might want to understand things like

- what is my mean fill rate per order
- what is my mean slippage per order
- etc…

which gets a lot more painful if I have to write various `missing`-wrangling things every 5 characters  
(side wish: would absolutely love to have `Over` in `DataFrames` for situations like these [expression functors (in particular: `over`) · Issue #3377 · JuliaData/DataFrames.jl · GitHub](https://github.com/JuliaData/DataFrames.jl/issues/3377))

```julia
julia> df
12×4 DataFrame
 Row │ order_id status order_px fill_px   
     │ Int64 Symbol Int64? Float64?  
─────┼───────────────────────────────────────
   1 │ 1 NEW 101 missing   
   2 │ 2 NEW 102 missing   
   3 │ 1 ACK missing missing   
   4 │ 3 NEW 98 missing   
   5 │ 4 NEW 97 missing   
   6 │ 2 ACK missing missing   
   7 │ 1 FILL missing 101.1
   8 │ 2 FILL missing 101.9
   9 │ 3 ACK missing missing   
  10 │ 4 ACK missing missing   
  11 │ 4 FILL missing 98.2
  12 │ 3 FILL missing 96.8

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 29, 2023, 1:51pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/200 "2023-11-29T13:51:01Z")

</div>

The `missing` here:

> [@adienes](#):
>
> ```julia
> 1 │ 1 NEW 101 missing   
> 
> ```

could easily be just `0.0` (every `NEW` order would have zero fill I suppose).  
As a sidenote, it may be messy to handle this data, but handling a “log-file” of a complex system such as a continuous-time double-auction service isn’t easy. Perhaps until enough infrastructure is built by people sweating the details and making good Packages.

---

<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:58pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/201 "2023-11-29T13:58:47Z")

</div>

> [@Dan](#):
>
> but handling a “log-file” of a complex system such as a continuous-time double-auction service isn’t easy.

tell me about it 🙂

but it could be eas_ier_ if I had a few tools and behaviors like I get in the python dataframe libs

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 29, 2023, 2:01pm UTC](https://discourse.julialang.org/t/why-are-missing-values-not-ignored-by-default/106756/202 "2023-11-29T14:01:32Z")

</div>

Seems like the root issue is there not being a distinguishing between “value unknown” and “no value”. Is there concretely something that DataFrames/Julia could do to provide that for you?

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

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