|
| 1 | +# How InMemoryDatasets treats missing values? |
| 2 | + |
| 3 | +## Every column supports `missing` |
| 4 | + |
| 5 | +The `Dataset()` constructor automatically converts each column of a data set to allow `missing` when constructs a data set. All algorithms in InMemoryDatasets are optimised to minimised the overhead of supporting `missing` type. |
| 6 | + |
| 7 | +## Functions which skip missing values |
| 8 | + |
| 9 | +When InMemoryDatasets loaded into a Julia session, the behaviour of the following functions will be changed in such a way that they will remove missing values if an `AbstractVector{Union{T, Missing}}` is passed as their argument. And it is the user responsibility to handle the situations where this is not desired. |
| 10 | + |
| 11 | +The following list summarises the details of how InMemoryDatasets removes/skips/ignores missing values (for the rest of this section `INTEGERS` refers to `{U/Int8, U/Int16, U/Int32, U/Int64}` and `FLOATS` refers to `{Float16, Float32, Float64}`): |
| 12 | + |
| 13 | +* `argmax` : For `INTEGERS`, `FLOATS`, `TimeType`, and `AbstractString` skip missing values. When all values are `missing`, it returns `missing`. |
| 14 | +* `argmin` : For `INTEGERS`, `FLOATS`, `TimeType`, and `AbstractString` skip missing values. When all values are `missing`, it returns `missing`. |
| 15 | +* `cummax` : For `INTEGERS`, `FLOATS`, and `TimeType` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 16 | +* `cummax!`: For `INTEGERS`, `FLOATS`, and `TimeType` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 17 | +* `cummin` : For `INTEGERS`, `FLOATS`, and `TimeType` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 18 | +* `cummin!`: For `INTEGERS`, `FLOATS`, and `TimeType` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 19 | +* `cumprod` : For `INTEGERS` and `FLOATS` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 20 | +* `cumprod!`: For `INTEGERS` and `FLOATS` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 21 | +* `cumsum` : For `INTEGERS` and `FLOATS` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 22 | +* `cumsum!` : For `INTEGERS` and `FLOATS` ignore missing values, however, by passing `missings = :skip` it jumps over missing values. When all values are `missing`, it returns the input. |
| 23 | +* `extrema` : For `INTEGERS`, `FLOATS`, and `TimeType` skip missing values. When all values are `missing`, it returns `(missing, missing)`. |
| 24 | +* `findmax` : For `INTEGERS`, `FLOATS`, `TimeType`, and `AbstractString` skip missing values. When all values are `missing`, it returns `(missing, missing)`. |
| 25 | +* `findmin` : For `INTEGERS`, `FLOATS`, `TimeType`, and `AbstractString` skip missing values. When all values are `missing`, it returns `(missing, missing)`. |
| 26 | +* `maximum` : For `INTEGERS`, `FLOATS`, `TimeType`, and `AbstractString` skip missing values. When all values are `missing`, it returns `missing`. |
| 27 | +* `mean` : For `INTEGERS` and `FLOATS` skip missing values. When all values are `missing`, it returns `missing` |
| 28 | +* `median` : For `INTEGERS` and `FLOATS` skip missing values. When all values are `missing`, it returns `missing` |
| 29 | +* `median!` : For `INTEGERS` and `FLOATS` skip missing values. When all values are `missing`, it returns `missing` |
| 30 | +* `minimum` : For `INTEGERS`, `FLOATS`, `TimeType`, and `AbstractString` skip missing values. When all values are `missing`, it returns `missing`. |
| 31 | +* `std` : For `INTEGERS` and `FLOATS` skip missing values. When all values are `missing`, it returns `missing` |
| 32 | +* `sum` : For `INTEGERS` and `FLOATS` skip missing values. When all values are `missing`, it returns `missing` |
| 33 | +* `var` : For `INTEGERS` and `FLOATS` skip missing values. When all values are `missing`, it returns `missing` |
| 34 | + |
| 35 | +```jldoctest |
| 36 | +julia> x = [1,1,missing] |
| 37 | +3-element Vector{Union{Missing, Int64}}: |
| 38 | + 1 |
| 39 | + 1 |
| 40 | + missing |
| 41 | +
|
| 42 | +julia> sum(x) |
| 43 | +2 |
| 44 | +
|
| 45 | +julia> mean(x) |
| 46 | +1.0 |
| 47 | +
|
| 48 | +julia> maximum(x) |
| 49 | +1 |
| 50 | +
|
| 51 | +julia> minimum(x) |
| 52 | +1 |
| 53 | +
|
| 54 | +julia> findmax(x) |
| 55 | +(1, 1) |
| 56 | +
|
| 57 | +julia> findmin(x) |
| 58 | +(1, 1) |
| 59 | +
|
| 60 | +julia> cumsum(x) |
| 61 | +3-element Vector{Union{Missing, Int64}}: |
| 62 | + 1 |
| 63 | + 2 |
| 64 | + 2 |
| 65 | +
|
| 66 | +julia> cumsum(x, missings = :skip) |
| 67 | +3-element Vector{Union{Missing, Int64}}: |
| 68 | + 1 |
| 69 | + 2 |
| 70 | + missing |
| 71 | +
|
| 72 | +julia> cumprod(x, missings = :skip) |
| 73 | +3-element Vector{Union{Missing, Int64}}: |
| 74 | + 1 |
| 75 | + 1 |
| 76 | + missing |
| 77 | +
|
| 78 | +julia> median(x) |
| 79 | +1.0 |
| 80 | +``` |
| 81 | + |
| 82 | +### Some remarks |
| 83 | + |
| 84 | +`var` and `std` will return `missing` when `dof = true` and an `AbstractVector{Union{T, Missing}}` of length one is passed as their argument. This is different from the behaviour of these functions defined in the `Statistics` package. |
| 85 | + |
| 86 | +```jldoctest |
| 87 | +julia> var(Union{Missing, Int}[1]) |
| 88 | +missing |
| 89 | +
|
| 90 | +julia> std(Union{Missing, Int}[1]) |
| 91 | +missing |
| 92 | +
|
| 93 | +julia> var([1]) # fallback to Statistics.var |
| 94 | +NaN |
| 95 | +
|
| 96 | +julia> std([1]) # fallback to Statistics.std |
| 97 | +NaN |
| 98 | +``` |
| 99 | + |
| 100 | +## Multithreaded functions |
| 101 | + |
| 102 | +The `sum`, `minimum`, and `maximum` functions also support the `threads` keyword argument. When it is set to `true`, they exploit all cores for calculation. |
| 103 | + |
| 104 | +## `topk`, `IMD.n`, and `IMD.nmissing` |
| 105 | + |
| 106 | +The following function is also exported by InMemoryDatasets: |
| 107 | + |
| 108 | +* `topk` : Return top(bottom) k values of a vector. It ignores `missing` values, unless all values are `missing` which it returns `[missing]`. |
| 109 | + |
| 110 | +and the following functions are not exported but are available via `dot` notation: |
| 111 | + |
| 112 | +* `InMemoryDatasets.n` or `IMD.n` : Return number of non-missing elements |
| 113 | +* `InMemoryDatasets.nmissing` or `IMD.nmissing` : Return number of `missing` elements |
| 114 | + |
| 115 | +```jldoctest |
| 116 | +julia> x = [13, 1, missing, 10] |
| 117 | +4-element Vector{Union{Missing, Int64}}: |
| 118 | + 13 |
| 119 | + 1 |
| 120 | + missing |
| 121 | + 10 |
| 122 | +
|
| 123 | +julia> topk(x, 2) |
| 124 | +2-element Vector{Int64}: |
| 125 | + 13 |
| 126 | + 10 |
| 127 | +
|
| 128 | +julia> topk(x, 2, rev = true) |
| 129 | +2-element Vector{Int64}: |
| 130 | + 1 |
| 131 | + 10 |
| 132 | +julia> IMD.n(x) |
| 133 | +3 |
| 134 | +
|
| 135 | +julia> IMD.nmissing(x) |
| 136 | +1 |
| 137 | +``` |
0 commit comments