@@ -15,8 +15,8 @@ The behavior of `missing` values follows one basic rule: `missing`
15
15
values * propagate* automatically when passed to standard operators and functions,
16
16
in particular mathematical functions. Uncertainty about the value of one of the operands
17
17
induces uncertainty about the result. In practice, this means an operation involving
18
- a ` missing ` value generally returns ` missing ` :
19
- ``` doctest
18
+ a ` missing ` value generally returns ` missing `
19
+ ``` jldoctest
20
20
julia> missing + 1
21
21
missing
22
22
@@ -25,7 +25,6 @@ missing
25
25
26
26
julia> abs(missing)
27
27
missing
28
-
29
28
```
30
29
31
30
As ` missing ` is a normal Julia object, this propagation rule only works
@@ -42,8 +41,8 @@ throws a `MethodError`, just like for any other type.
42
41
43
42
Standard equality and comparison operators follow the propagation rule presented
44
43
above: if any of the operands is ` missing ` , the result is ` missing ` .
45
- Here are a few examples:
46
- ``` doctest
44
+ Here are a few examples
45
+ ``` jldoctest
47
46
julia> missing == 1
48
47
missing
49
48
@@ -55,7 +54,6 @@ missing
55
54
56
55
julia> 2 >= missing
57
56
missing
58
-
59
57
```
60
58
61
59
In particular, note that ` missing == missing ` returns ` missing ` , so ` == ` cannot
@@ -65,8 +63,8 @@ use [`ismissing(x)`](@ref).
65
63
Special comparison operators [ ` isequal ` ] ( @ref ) and [ ` === ` ] ( @ref ) are exceptions
66
64
to the propagation rule: they always return a ` Bool ` value, even in the presence
67
65
of ` missing ` values, considering ` missing ` as equal to ` missing ` and as different
68
- from any other value. They can therefore be used to test whether a value is ` missing ` :
69
- ``` doctest
66
+ from any other value. They can therefore be used to test whether a value is ` missing `
67
+ ``` jldoctest
70
68
julia> missing === 1
71
69
false
72
70
78
76
79
77
julia> isequal(missing, missing)
80
78
true
81
-
82
79
```
83
80
84
81
The [ ` isless ` ] ( @ref ) operator is another exception: ` missing ` is considered
85
82
as greater than any other value. This operator is used by [ ` sort ` ] ( @ref ) ,
86
83
which therefore places ` missing ` values after all other values.
87
-
88
- ``` doctest
84
+ ``` jldoctest
89
85
julia> isless(1, missing)
90
86
true
91
87
94
90
95
91
julia> isless(missing, missing)
96
92
false
97
-
98
93
```
99
94
100
95
## Logical operators
@@ -111,8 +106,8 @@ via concrete examples.
111
106
Let us illustrate this principle with the logical "or" operator [ ` | ` ] ( @ref ) .
112
107
Following the rules of boolean logic, if one of the operands is ` true ` ,
113
108
the value of the other operand does not have an influence on the result,
114
- which will always be ` true ` :
115
- ``` doctest
109
+ which will always be ` true `
110
+ ``` jldoctest
116
111
julia> true | true
117
112
true
118
113
@@ -121,28 +116,26 @@ true
121
116
122
117
julia> false | true
123
118
true
124
-
125
119
```
126
120
127
121
Based on this observation, we can conclude that if one of the operands is ` true `
128
122
and the other ` missing ` , we know that the result is ` true ` in spite of the
129
123
uncertainty about the actual value of one of the operands. If we had
130
124
been able to observe the actual value of the second operand, it could only be
131
125
` true ` or ` false ` , and in both cases the result would be ` true ` . Therefore,
132
- in this particular case, missingness does * not* propagate:
133
- ``` doctest
126
+ in this particular case, missingness does * not* propagate
127
+ ``` jldoctest
134
128
julia> true | missing
135
129
true
136
130
137
131
julia> missing | true
138
132
true
139
-
140
133
```
141
134
142
135
On the contrary, if one of the operands is ` false ` , the result could be either
143
136
` true ` or ` false ` depending on the value of the other operand. Therefore,
144
- if that operand is ` missing ` , the result has to be ` missing ` too:
145
- ``` doctest
137
+ if that operand is ` missing ` , the result has to be ` missing ` too
138
+ ``` jldoctest
146
139
julia> false | true
147
140
true
148
141
@@ -157,14 +150,13 @@ missing
157
150
158
151
julia> missing | false
159
152
missing
160
-
161
153
```
162
154
163
155
The behavior of the logical "and" operator [ ` & ` ] ( @ref ) is similar to that of the
164
156
` | ` operator, with the difference that missingness does not propagate when
165
157
one of the operands is ` false ` . For example, when that is the case of the first
166
- operand:
167
- ``` doctest
158
+ operand
159
+ ``` jldoctest
168
160
julia> false & false
169
161
false
170
162
@@ -173,12 +165,11 @@ false
173
165
174
166
julia> false & missing
175
167
false
176
-
177
168
```
178
169
179
170
On the other hand, missingness propagates when one of the operands is ` true ` ,
180
- for example the first one:
181
- ``` doctest
171
+ for example the first one
172
+ ``` jldoctest
182
173
julia> true & true
183
174
true
184
175
@@ -187,7 +178,6 @@ false
187
178
188
179
julia> true & missing
189
180
missing
190
-
191
181
```
192
182
193
183
Finally, the "exclusive or" logical operator [ ` xor ` ] ( @ref ) always propagates
@@ -202,20 +192,19 @@ Control flow operators including [`if`](@ref), [`while`](@ref) and the
202
192
do not allow for missing values. This is because of the uncertainty about whether
203
193
the actual value would be ` true ` or ` false ` if we could observe it,
204
194
which implies that we do not know how the program should behave. A ` TypeError `
205
- is thrown as soon as a ` missing ` value is encountered in this context:
206
- ``` doctest
195
+ is thrown as soon as a ` missing ` value is encountered in this context
196
+ ``` jldoctest
207
197
julia> if missing
208
198
println("here")
209
199
end
210
200
ERROR: TypeError: non-boolean (Missing) used in boolean context
211
-
212
201
```
213
202
214
203
For the same reason, contrary to logical operators presented above,
215
204
the short-circuiting boolean operators [ ` && ` ] ( @ref ) and [ ` || ` ] ( @ref ) do not
216
205
allow for ` missing ` values in situations where the value of the operand
217
- determines whether the next operand is evaluated or not. For example:
218
- ``` doctest
206
+ determines whether the next operand is evaluated or not. For example
207
+ ``` jldoctest
219
208
julia> missing || false
220
209
ERROR: TypeError: non-boolean (Missing) used in boolean context
221
210
@@ -224,31 +213,28 @@ ERROR: TypeError: non-boolean (Missing) used in boolean context
224
213
225
214
julia> true && missing && false
226
215
ERROR: TypeError: non-boolean (Missing) used in boolean context
227
-
228
216
```
229
217
230
218
On the other hand, no error is thrown when the result can be determined without
231
219
the ` missing ` values. This is the case when the code short-circuits
232
220
before evaluating the ` missing ` operand, and when the ` missing ` operand is the
233
- last one:
234
- ``` doctest
221
+ last one
222
+ ``` jldoctest
235
223
julia> true && missing
236
224
missing
237
225
238
226
julia> false && missing
239
227
false
240
-
241
228
```
242
229
243
230
## Arrays With Missing Values
244
231
245
- Arrays containing missing values can be created like other arrays:
246
- ``` doctest
232
+ Arrays containing missing values can be created like other arrays
233
+ ``` jldoctest
247
234
julia> [1, missing]
248
235
2-element Array{Union{Missing, Int64},1}:
249
236
1
250
237
missing
251
-
252
238
```
253
239
254
240
As this example shows, the element type of such arrays is ` Union{Missing, T} ` ,
@@ -260,20 +246,19 @@ of the entry (i.e. whether it is `Missing` or `T`).
260
246
261
247
Uninitialized arrays allowing for missing values can be constructed with the
262
248
standard syntax. By default, arrays with an [ ` isbits ` ] ( @ref ) element type are
263
- filled with ` missing ` values:
264
- ``` doctest
249
+ filled with ` missing ` values
250
+ ``` jldoctest
265
251
julia> Array{Union{Missing, Int}}(uninitialized, 2, 3)
266
252
2×3 Array{Union{Missing, Int64},2}:
267
253
missing missing missing
268
254
missing missing missing
269
-
270
255
```
271
256
272
257
An array allowing for ` missing ` values but which does not contain any such value
273
258
can be converted back to an array which does not allow for missing values using
274
259
[ ` convert ` ] ( @ref ) . If the array contains ` missing ` values, a ` MethodError ` is thrown
275
- during conversion:
276
- ``` doctest
260
+ during conversion
261
+ ``` jldoctest
277
262
julia> x = Union{Missing, String}["a", "b"]
278
263
2-element Array{Union{Missing, String},1}:
279
264
"a"
@@ -294,28 +279,26 @@ ERROR: MethodError: Cannot `convert` an object of type Missing to an object of t
294
279
This may have arisen from a call to the constructor String(...),
295
280
since type constructors fall back to convert methods.
296
281
Stacktrace:
297
- [...]
282
+ [...
298
283
```
299
284
## Skipping Missing Values
300
285
301
286
Since ` missing ` values propagate with standard mathematical operators, reduction
302
- functions return ` missing ` when called on arrays which contain missing values:
303
- ``` doctest
287
+ functions return ` missing ` when called on arrays which contain missing values
288
+ ``` jldoctest
304
289
julia> sum([1, missing])
305
290
missing
306
-
307
291
```
308
292
309
- In this situation, use the [ ` skipmissing ` ] ( @ref ) function to skip missing values:
310
- ``` doctest
293
+ In this situation, use the [ ` skipmissing ` ] ( @ref ) function to skip missing values
294
+ ``` jldoctest
311
295
julia> sum(skipmissing([1, missing]))
312
296
1
313
-
314
297
```
315
298
316
299
This convenience function returns an iterator which filters out ` missing ` values
317
- efficiently. It can therefore be used with any function which supports iterators:
318
- ``` doctest
300
+ efficiently. It can therefore be used with any function which supports iterators
301
+ ``` jldoctest
319
302
julia> maximum(skipmissing([3, missing, 2, 1]))
320
303
3
321
304
@@ -324,17 +307,15 @@ julia> mean(skipmissing([3, missing, 2, 1]))
324
307
325
308
julia> mapreduce(sqrt, +, skipmissing([3, missing, 2, 1]))
326
309
4.146264369941973
327
-
328
310
```
329
311
330
- Use [ ` collect ` ] ( @ref ) to extract non-` missing ` values and store them in an array:
331
- ``` doctest
312
+ Use [ ` collect ` ] ( @ref ) to extract non-` missing ` values and store them in an array
313
+ ``` jldoctest
332
314
julia> collect(skipmissing([3, missing, 2, 1]))
333
315
3-element Array{Int64,1}:
334
316
3
335
317
2
336
318
1
337
-
338
319
```
339
320
340
321
## Logical Operations on Arrays
@@ -345,8 +326,8 @@ the [`==`](@ref) operator return `missing` whenever the result cannot be
345
326
determined without knowing the actual value of the ` missing ` entry. In practice,
346
327
this means that ` missing ` is returned if all non-missing values of the compared
347
328
arrays are equal, but one or both arrays contain missing values (possibly at
348
- different positions):
349
- ``` doctest
329
+ different positions)
330
+ ``` jldoctest
350
331
julia> [1, missing] == [2, missing]
351
332
false
352
333
@@ -355,23 +336,21 @@ missing
355
336
356
337
julia> [1, 2, missing] == [1, missing, 2]
357
338
missing
358
-
359
339
```
360
340
361
341
As for single values, use [ ` isequal ` ] ( @ref ) to treat ` missing ` values as equal
362
- to other ` missing ` values but different from non-missing values:
363
- ``` doctest
342
+ to other ` missing ` values but different from non-missing values
343
+ ``` jldoctest
364
344
julia> isequal([1, missing], [1, missing])
365
345
true
366
346
367
347
julia> isequal([1, 2, missing], [1, missing, 2])
368
348
false
369
-
370
349
```
371
350
372
351
Functions [ ` any ` ] ( @ref ) and [ ` all ` ] ( @ref ) also follow the rules of
373
- three-valued logic, returning ` missing ` when the result cannot be determined:
374
- ``` doctest
352
+ three-valued logic, returning ` missing ` when the result cannot be determined
353
+ ``` jldoctest
375
354
julia> all([true, missing])
376
355
missing
377
356
383
362
384
363
julia> any([false, missing])
385
364
missing
386
-
387
365
```
0 commit comments