-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnep-0012-missing-data.html
1742 lines (1540 loc) · 129 KB
/
nep-0012-missing-data.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" data-content_root="./" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>NEP 12 — Missing data functionality in NumPy — NumPy Enhancement Proposals</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<!-- So that users can add custom icons -->
<script src="_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="_static/documentation_options.js?v=7f41d439"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'nep-0012-missing-data';</script>
<link rel="icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="NEP 21 — Simplified and explicit advanced indexing" href="nep-0021-advanced-indexing.html" />
<link rel="prev" title="NEP 11 — Deferred UFunc evaluation" href="nep-0011-deferred-ufunc-evaluation.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="" />
<meta name="docbuild:last-update" content="Feb 13, 2025"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="content.html">
<img src="_static/numpylogo.svg" class="logo__image only-light" alt="NumPy Enhancement Proposals - Home"/>
<img src="_static/numpylogo_dark.svg" class="logo__image only-dark pst-js-only" alt="NumPy Enhancement Proposals - Home"/>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="navbar-persistent--mobile">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav"><ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="scope.html">The Scope of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="roadmap.html">Current roadmap</a></li>
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 has-children"><a class="reference internal" href="meta.html">Meta-NEPs (NEPs about NEPs or active Processes)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0000.html">NEP 0 — Purpose and process</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0023-backwards-compatibility.html">NEP 23 — Backwards compatibility and deprecation policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0036-fair-play.html">NEP 36 — Fair play</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0045-c_style_guide.html">NEP 45 — C style guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0046-sponsorship-guidelines.html">NEP 46 — NumPy sponsorship guidelines</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0048-spending-project-funds.html">NEP 48 — Spending NumPy project funds</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-template.html">NEP X — Template and instructions</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="provisional.html">Provisional NEPs (provisionally accepted; interface may change)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="simple">
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="accepted.html">Accepted NEPs (implementation in progress)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0041-improved-dtype-support.html">NEP 41 — First step towards a new datatype system</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0042-new-dtypes.html">NEP 42 — New and extensible DTypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0044-restructuring-numpy-docs.html">NEP 44 — Restructuring the NumPy documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0051-scalar-representation.html">NEP 51 — Changing the representation of NumPy scalars</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="open.html">Open NEPs (under consideration)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0043-extensible-ufuncs.html">NEP 43 — Enhancing the extensibility of UFuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0053-c-abi-evolution.html">NEP 53 — Evolving the NumPy C-API for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0054-simd-cpp-highway.html">NEP 54 — SIMD infrastructure evolution: adopting Google Highway when moving to C++?</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="finished.html">Finished NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0001-npy-format.html">NEP 1 — A simple file format for NumPy arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0005-generalized-ufuncs.html">NEP 5 — Generalized universal functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0007-datetime-proposal.html">NEP 7 — A proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0010-new-iterator-ufunc.html">NEP 10 — Optimizing iterator/UFunc performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0013-ufunc-overrides.html">NEP 13 — A mechanism for overriding Ufuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0014-dropping-python2.7-proposal.html">NEP 14 — Plan for dropping Python 2.7 support</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0015-merge-multiarray-umath.html">NEP 15 — Merging multiarray and umath</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0018-array-function-protocol.html">NEP 18 — A dispatch mechanism for NumPy's high level array functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0019-rng-policy.html">NEP 19 — Random number generator policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0020-gufunc-signature-enhancement.html">NEP 20 — Expansion of generalized universal function signatures</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0022-ndarray-duck-typing-overview.html">NEP 22 — Duck typing for NumPy arrays – high level overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0027-zero-rank-arrarys.html">NEP 27 — Zero rank arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0028-website-redesign.html">NEP 28 — numpy.org website redesign</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0029-deprecation_policy.html">NEP 29 — Recommend Python and NumPy version support as a community policy standard</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0032-remove-financial-functions.html">NEP 32 — Remove the financial functions from NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0034-infer-dtype-is-object.html">NEP 34 — Disallow inferring ``dtype=object`` from sequences</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0035-array-creation-dispatch-with-array-function.html">NEP 35 — Array creation dispatching with __array_function__</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0038-SIMD-optimizations.html">NEP 38 — Using SIMD optimization instructions for performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0040-legacy-datatype-impl.html">NEP 40 — Legacy datatype implementation in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0049.html">NEP 49 — Data allocation strategies</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0050-scalar-promotion.html">NEP 50 — Promotion rules for Python scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0052-python-api-cleanup.html">NEP 52 — Python API cleanup for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0055-string_dtype.html">NEP 55 — Add a UTF-8 variable-width string DType to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0056-array-api-main-namespace.html">NEP 56 — Array API standard support in NumPy's main namespace</a></li>
</ul>
</details></li>
<li class="toctree-l1 current active has-children"><a class="reference internal" href="deferred.html">Deferred and Superseded NEPs</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="nep-0002-warnfix.html">NEP 2 — A proposal to build numpy without warning with a big set of warning flags</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0003-math_config_clean.html">NEP 3 — Cleaning the math configuration of numpy.core</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0004-datetime-proposal3.html">NEP 4 — A (third) proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0006-newbugtracker.html">NEP 6 — Replacing Trac with a different bug tracker</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0008-groupby_additions.html">NEP 8 — A proposal for adding groupby functionality to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0009-structured_array_extensions.html">NEP 9 — Structured array extensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0011-deferred-ufunc-evaluation.html">NEP 11 — Deferred UFunc evaluation</a></li>
<li class="toctree-l2 current active"><a class="current reference internal" href="#">NEP 12 — Missing data functionality in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0021-advanced-indexing.html">NEP 21 — Simplified and explicit advanced indexing</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0024-missing-data-2.html">NEP 24 — Missing data functionality - alternative 1 to NEP 12</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0025-missing-data-3.html">NEP 25 — NA support via special dtypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0026-missing-data-summary.html">NEP 26 — Summary of missing data NEPs and discussion</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0030-duck-array-protocol.html">NEP 30 — Duck typing for NumPy arrays - implementation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0031-uarray.html">NEP 31 — Context-local and global overrides of the NumPy API</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0037-array-module.html">NEP 37 — A dispatch protocol for NumPy-like modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0047-array-api-standard.html">NEP 47 — Adopting the array API standard</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="rejected.html">Rejected and Withdrawn NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0016-abstract-array.html">NEP 16 — An abstract base class for identifying "duck arrays"</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0017-split-out-maskedarray.html">NEP 17 — Split out masked arrays</a></li>
</ul>
</details></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item">
<div id="ethical-ad-placement"
class="flat"
data-ea-publisher="readthedocs"
data-ea-type="readthedocs-sidebar"
data-ea-manual="true">
</div></div>
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="content.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">Roadmap & NumPy enhancement proposals</a></li>
<li class="breadcrumb-item"><a href="deferred.html" class="nav-link">Deferred and Superseded NEPs</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">NEP 12 — Missing data functionality in NumPy</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="nep-12-missing-data-functionality-in-numpy">
<span id="nep12"></span><h1><a class="toc-backref" href="#id1" role="doc-backlink">NEP 12 — Missing data functionality in NumPy</a><a class="headerlink" href="#nep-12-missing-data-functionality-in-numpy" title="Link to this heading">#</a></h1>
<dl class="field-list simple">
<dt class="field-odd">Author<span class="colon">:</span></dt>
<dd class="field-odd"><p>Mark Wiebe <<a class="reference external" href="mailto:mwwiebe%40gmail.com">mwwiebe<span>@</span>gmail<span>.</span>com</a>></p>
</dd>
<dt class="field-even">Copyright<span class="colon">:</span></dt>
<dd class="field-even"><p>Copyright 2011 by Enthought, Inc</p>
</dd>
<dt class="field-odd">License<span class="colon">:</span></dt>
<dd class="field-odd"><p>CC By-SA 3.0 (<a class="reference external" href="https://creativecommons.org/licenses/by-sa/3.0/">https://creativecommons.org/licenses/by-sa/3.0/</a>)</p>
</dd>
<dt class="field-even">Date<span class="colon">:</span></dt>
<dd class="field-even"><p>2011-06-23</p>
</dd>
<dt class="field-odd">Status<span class="colon">:</span></dt>
<dd class="field-odd"><p>Deferred</p>
</dd>
</dl>
<section id="table-of-contents">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">Table of contents</a><a class="headerlink" href="#table-of-contents" title="Link to this heading">#</a></h2>
<nav class="contents" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#nep-12-missing-data-functionality-in-numpy" id="id1">NEP 12 — Missing data functionality in NumPy</a></p>
<ul>
<li><p><a class="reference internal" href="#table-of-contents" id="id2">Table of contents</a></p></li>
<li><p><a class="reference internal" href="#abstract" id="id3">Abstract</a></p></li>
<li><p><a class="reference internal" href="#definition-of-missing-data" id="id4">Definition of missing data</a></p>
<ul>
<li><p><a class="reference internal" href="#unknown-yet-existing-data-na" id="id5">Unknown yet existing data (NA)</a></p></li>
<li><p><a class="reference internal" href="#data-that-doesn-t-exist-or-is-being-skipped-ignore" id="id6">Data that doesn’t exist or is being skipped (IGNORE)</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#implementation-techniques-for-missing-values" id="id7">Implementation techniques for missing values</a></p>
<ul>
<li><p><a class="reference internal" href="#bit-patterns-signalling-missing-values-bitpattern" id="id8">Bit patterns signalling missing values (bitpattern)</a></p></li>
<li><p><a class="reference internal" href="#boolean-masks-signalling-missing-values-mask" id="id9">Boolean masks signalling missing values (mask)</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#glossary-of-terms" id="id10">Glossary of terms</a></p></li>
<li><p><a class="reference internal" href="#missing-values-as-seen-in-python" id="id11">Missing values as seen in Python</a></p>
<ul>
<li><p><a class="reference internal" href="#working-with-missing-values" id="id12">Working with missing values</a></p></li>
<li><p><a class="reference internal" href="#accessing-a-boolean-mask" id="id13">Accessing a boolean mask</a></p></li>
<li><p><a class="reference internal" href="#creating-na-masked-arrays" id="id14">Creating NA-masked arrays</a></p></li>
<li><p><a class="reference internal" href="#na-masks-when-constructing-from-lists" id="id15">Na-masks when constructing from lists</a></p></li>
<li><p><a class="reference internal" href="#mask-implementation-details" id="id16">Mask implementation details</a></p></li>
<li><p><a class="reference internal" href="#new-ndarray-methods" id="id17">New ndarray methods</a></p></li>
<li><p><a class="reference internal" href="#element-wise-ufuncs-with-missing-values" id="id18">Element-wise ufuncs with missing values</a></p></li>
<li><p><a class="reference internal" href="#reduction-ufuncs-with-missing-values" id="id19">Reduction ufuncs with missing values</a></p></li>
<li><p><a class="reference internal" href="#parameterized-na-data-types" id="id20">Parameterized NA data types</a></p></li>
<li><p><a class="reference internal" href="#future-expansion-to-multi-na-payloads" id="id21">Future expansion to multi-NA payloads</a></p></li>
<li><p><a class="reference internal" href="#differences-with-numpy-ma" id="id22">Differences with numpy.ma</a></p></li>
<li><p><a class="reference internal" href="#boolean-indexing" id="id23">Boolean indexing</a></p></li>
<li><p><a class="reference internal" href="#pep-3118" id="id24">PEP 3118</a></p></li>
<li><p><a class="reference internal" href="#cython" id="id25">Cython</a></p></li>
<li><p><a class="reference internal" href="#hard-masks" id="id26">Hard masks</a></p></li>
<li><p><a class="reference internal" href="#shared-masks" id="id27">Shared masks</a></p></li>
<li><p><a class="reference internal" href="#interaction-with-pre-existing-c-api-usage" id="id28">Interaction with pre-existing C API usage</a></p>
<ul>
<li><p><a class="reference internal" href="#numpy-documentation-how-to-extend-numpy" id="id29">NumPy documentation - how to extend NumPy</a></p></li>
<li><p><a class="reference internal" href="#tutorial-from-cython-website" id="id30">Tutorial from Cython website</a></p></li>
<li><p><a class="reference internal" href="#numerical-python-jpl-website" id="id31">Numerical Python - JPL website</a></p></li>
</ul>
</li>
</ul>
</li>
<li><p><a class="reference internal" href="#c-implementation-details" id="id32">C implementation details</a></p>
<ul>
<li><p><a class="reference internal" href="#mask-binary-format" id="id33">Mask binary format</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#c-iterator-api-changes-iteration-with-masks" id="id34">C iterator API changes: iteration with masks</a></p>
<ul>
<li><p><a class="reference internal" href="#iterator-mask-features" id="id35">Iterator mask features</a></p></li>
<li><p><a class="reference internal" href="#iterator-na-array-features" id="id36">Iterator NA-array features</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#rejected-alternative" id="id37">Rejected alternative</a></p>
<ul>
<li><p><a class="reference internal" href="#parameterized-data-type-which-adds-additional-memory-for-the-na-flag" id="id38">Parameterized data type which adds additional memory for the NA flag</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#acknowledgments" id="id39">Acknowledgments</a></p></li>
</ul>
</li>
</ul>
</nav>
</section>
<section id="abstract">
<h2><a class="toc-backref" href="#id3" role="doc-backlink">Abstract</a><a class="headerlink" href="#abstract" title="Link to this heading">#</a></h2>
<p>Users interested in dealing with missing data within NumPy are generally
pointed to the masked array subclass of the ndarray, known
as ‘numpy.ma’. This class has a number of users who depend strongly
on its capabilities, but people who are accustomed to the deep integration
of the missing data placeholder “NA” in the R project and others who
find the programming interface challenging or inconsistent tend not
to use it.</p>
<p>This NEP proposes to integrate a mask-based missing data solution
into NumPy, with an additional bitpattern-based missing data solution
that can be implemented concurrently or later integrating seamlessly
with the mask-based solution.</p>
<p>The mask-based solution and the bitpattern-based solutions in this
proposal offer the exact same missing value abstraction, with several
differences in performance, memory overhead, and flexibility.</p>
<p>The mask-based solution is more flexible, supporting all behaviors of the
bitpattern-based solution, but leaving the hidden values untouched
whenever an element is masked.</p>
<p>The bitpattern-based solution requires less memory, is bit-level
compatible with the 64-bit floating point representation used in R, but
does not preserve the hidden values and in fact requires stealing at
least one bit pattern from the underlying dtype to represent the missing
value NA.</p>
<p>Both solutions are generic in the sense that they can be used with
custom data types very easily, with no effort in the case of the masked
solution, and with the requirement that a bit pattern to sacrifice be
chosen in the case of the bitpattern solution.</p>
</section>
<section id="definition-of-missing-data">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Definition of missing data</a><a class="headerlink" href="#definition-of-missing-data" title="Link to this heading">#</a></h2>
<p>In order to be able to develop an intuition about what computation
will be done by various NumPy functions, a consistent conceptual
model of what a missing element means must be applied.
Ferreting out the behaviors people need or want when they are working
with “missing data” seems to be tricky, but I believe that it boils
down to two different ideas, each of which is internally self-consistent.</p>
<p>One of them, the “unknown yet existing data” interpretation, can be applied
rigorously to all computations, while the other makes sense for
some statistical operations like standard deviation but not for
linear algebra operations like matrix product.
Thus, making “unknown yet existing data” be the default interpretation
is superior, providing a consistent model across all computations,
and for those operations where the other interpretation makes sense,
an optional parameter “skipna=” can be added.</p>
<p>For people who want the other interpretation to be default, a mechanism
proposed elsewhere for customizing subclass ufunc behavior with a
_numpy_ufunc_ member function would allow a subclass with a different
default to be created.</p>
<section id="unknown-yet-existing-data-na">
<h3><a class="toc-backref" href="#id5" role="doc-backlink">Unknown yet existing data (NA)</a><a class="headerlink" href="#unknown-yet-existing-data-na" title="Link to this heading">#</a></h3>
<p>This is the approach taken in the R project, defining a missing element
as something which does have a valid value which isn’t known, or is
NA (not available). This proposal adopts this behavior as the
default for all operations involving missing values.</p>
<p>In this interpretation, nearly any computation with a missing input produces
a missing output. For example, ‘sum(a)’ would produce a missing value
if ‘a’ contained just one missing element. When the output value does
not depend on one of the inputs, it is reasonable to output a value
that is not NA, such as logical_and(NA, False) == False.</p>
<p>Some more complex arithmetic operations, such as matrix products, are
well defined with this interpretation, and the result should be
the same as if the missing values were NaNs. Actually implementing
such things to the theoretical limit is probably not worth it,
and in many cases either raising an exception or returning all
missing values may be preferred to doing precise calculations.</p>
</section>
<section id="data-that-doesn-t-exist-or-is-being-skipped-ignore">
<h3><a class="toc-backref" href="#id6" role="doc-backlink">Data that doesn’t exist or is being skipped (IGNORE)</a><a class="headerlink" href="#data-that-doesn-t-exist-or-is-being-skipped-ignore" title="Link to this heading">#</a></h3>
<p>Another useful interpretation is that the missing elements should be
treated as if they didn’t exist in the array, and the operation should
do its best to interpret what that means according to the data
that’s left. In this case, ‘mean(a)’ would compute the mean of just
the values that are available, adjusting both the sum and count it
uses based on which values are missing. To be consistent, the mean of
an array of all missing values must produce the same result as the
mean of a zero-sized array without missing value support.</p>
<p>This kind of data can arise when conforming sparsely sampled data
into a regular sampling pattern, and is a useful interpretation to
use when attempting to get best-guess answers for many statistical queries.</p>
<p>In R, many functions take a parameter “na.rm=T” which means to treat
the data as if the NA values are not part of the data set. This proposal
defines a standard parameter “skipna=True” for this same purpose.</p>
</section>
</section>
<section id="implementation-techniques-for-missing-values">
<h2><a class="toc-backref" href="#id7" role="doc-backlink">Implementation techniques for missing values</a><a class="headerlink" href="#implementation-techniques-for-missing-values" title="Link to this heading">#</a></h2>
<p>In addition to there being two different interpretations of missing values,
there are two different commonly used implementation techniques for
missing values. While there are some differing default behaviors between
existing implementations of the techniques, I believe that the design
choices made in a new implementation must be made based on their merits,
not by rote copying of previous designs.</p>
<p>Both masks and bitpatterns have different strong and weak points,
depending on the application context. This NEP thus proposes to implement
both. To enable the writing of generic “missing value” code which does
not have to worry about whether the arrays it is using have taken one
or the other approach, the missing value semantics will be identical
for the two implementations.</p>
<section id="bit-patterns-signalling-missing-values-bitpattern">
<h3><a class="toc-backref" href="#id8" role="doc-backlink">Bit patterns signalling missing values (bitpattern)</a><a class="headerlink" href="#bit-patterns-signalling-missing-values-bitpattern" title="Link to this heading">#</a></h3>
<p>One or more patterns of bits, for example a NaN with
a particular payload, are chosen to represent the missing value
placeholder NA.</p>
<p>A consequence of this approach is that assigning NA changes the bits
holding the value, so that value is gone.</p>
<p>Additionally, for some types such as integers, a good and proper value
must be sacrificed to enable this functionality.</p>
</section>
<section id="boolean-masks-signalling-missing-values-mask">
<h3><a class="toc-backref" href="#id9" role="doc-backlink">Boolean masks signalling missing values (mask)</a><a class="headerlink" href="#boolean-masks-signalling-missing-values-mask" title="Link to this heading">#</a></h3>
<p>A mask is a parallel array of booleans, either one byte per element or
one bit per element, allocated alongside the existing array data. In this
NEP, the convention is chosen that True means the element is valid
(unmasked), and False means the element is NA.</p>
<p>By taking care when writing any C algorithm that works with values
and masks together, it is possible to have the memory for a value
that is masked never be written to. This feature allows multiple
simultaneous views of the same data with different choices of what
is missing, a feature requested by many people on the mailing list.</p>
<p>This approach places no limitations on the values of the underlying
data type, it may take on any binary pattern without affecting the
NA behavior.</p>
</section>
</section>
<section id="glossary-of-terms">
<h2><a class="toc-backref" href="#id10" role="doc-backlink">Glossary of terms</a><a class="headerlink" href="#glossary-of-terms" title="Link to this heading">#</a></h2>
<p>Because the above discussions of the different concepts and their
relationships are tricky to understand, here are more succinct
definitions of the terms used in this NEP.</p>
<dl class="simple">
<dt>NA (Not Available/Propagate)</dt><dd><p>A placeholder for a value which is unknown to computations. That
value may be temporarily hidden with a mask, may have been lost
due to hard drive corruption, or gone for any number of reasons.
For sums and products this means to produce NA if any of the inputs
are NA. This is the same as NA in the R project.</p>
</dd>
<dt>IGNORE (Ignore/Skip)</dt><dd><p>A placeholder which should be treated by computations as if no value does
or could exist there. For sums, this means act as if the value
were zero, and for products, this means act as if the value were one.
It’s as if the array were compressed in some fashion to not include
that element.</p>
</dd>
<dt>bitpattern</dt><dd><p>A technique for implementing either NA or IGNORE, where a particular
set of bit patterns are chosen from all the possible bit patterns of the
value’s data type to signal that the element is NA or IGNORE.</p>
</dd>
<dt>mask</dt><dd><p>A technique for implementing either NA or IGNORE, where a
boolean or enum array parallel to the data array is used to signal
which elements are NA or IGNORE.</p>
</dd>
<dt>numpy.ma</dt><dd><p>The existing implementation of a particular form of masked arrays,
which is part of the NumPy codebase.</p>
</dd>
<dt>Python API</dt><dd><p>All the interface mechanisms that are exposed to Python code
for using missing values in NumPy. This API is designed to be
Pythonic and fit into the way NumPy works as much as possible.</p>
</dd>
<dt>C API</dt><dd><p>All the implementation mechanisms exposed for CPython extensions
written in C that want to support NumPy missing value support.
This API is designed to be as natural as possible in C, and
is usually prioritizes flexibility and high performance.</p>
</dd>
</dl>
</section>
<section id="missing-values-as-seen-in-python">
<h2><a class="toc-backref" href="#id11" role="doc-backlink">Missing values as seen in Python</a><a class="headerlink" href="#missing-values-as-seen-in-python" title="Link to this heading">#</a></h2>
<section id="working-with-missing-values">
<h3><a class="toc-backref" href="#id12" role="doc-backlink">Working with missing values</a><a class="headerlink" href="#working-with-missing-values" title="Link to this heading">#</a></h3>
<p>NumPy will gain a global singleton called numpy.NA, similar to None,
but with semantics reflecting its status as a missing value. In particular,
trying to treat it as a boolean will raise an exception, and comparisons
with it will produce numpy.NA instead of True or False. These basics are
adopted from the behavior of the NA value in the R project. To dig
deeper into the ideas, <a class="reference external" href="https://en.wikipedia.org/wiki/Ternary_logic#Kleene_logic">https://en.wikipedia.org/wiki/Ternary_logic#Kleene_logic</a>
provides a starting point.</p>
<p>For example,:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">],</span> <span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">array([1., 2., NA, 7.], maskna=True)</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'NA'</span><span class="p">)</span>
<span class="go">array([1., 2., NA, 7.], dtype='NA[<f8]')</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'NA[f4]'</span><span class="p">)</span>
<span class="go">array([1., 2., NA, 7.], dtype='NA[<f4]')</span>
</pre></div>
</div>
<p>produce arrays with values [1.0, 2.0, <inaccessible>, 7.0] /
mask [Exposed, Exposed, Hidden, Exposed], and
values [1.0, 2.0, <NA bitpattern>, 7.0] for the masked and
NA dtype versions respectively.</p>
<p>The np.NA singleton may accept a dtype= keyword parameter, indicating
that it should be treated as an NA of a particular data type. This is also
a mechanism for preserving the dtype in a NumPy scalar-like fashion.
Here’s what this looks like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">],</span> <span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">NA(dtype='<f8')</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'NA[f8]'</span><span class="p">))</span>
<span class="go">NA(dtype='NA[<f8]')</span>
</pre></div>
</div>
<p>Assigning a value to an array always causes that element to not be NA,
transparently unmasking it if necessary. Assigning numpy.NA to the array
masks that element or assigns the NA bitpattern for the particular dtype.
In the mask-based implementation, the storage behind a missing value may never
be accessed in any way, other than to unmask it by assigning its value.</p>
<p>To test if a value is missing, the function “np.isna(arr[0])” will
be provided. One of the key reasons for the NumPy scalars is to allow
their values into dictionaries.</p>
<p>All operations which write to masked arrays will not affect the value
unless they also unmask that value. This allows the storage behind
masked elements to still be relied on if they are still accessible
from another view which doesn’t have them masked. For example, the
following was run on the missingdata work-in-progress branch:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">b</span>
<span class="go">array([1, 2], maskna=True)</span>
<span class="gp">>>> </span><span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span>
<span class="gp">>>> </span><span class="n">b</span>
<span class="go">array([NA, 2], maskna=True)</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([1, 2])</span>
<span class="gp">>>> </span><span class="c1"># The underlying number 1 value in 'a[0]' was untouched</span>
</pre></div>
</div>
<p>Copying values between the mask-based implementation and the
bitpattern implementation will transparently do the correct thing,
turning the bitpattern into a masked value, or a masked value
into the bitpattern where appropriate. The one exception is
if a valid value in a masked array happens to have the NA bitpattern,
copying this value to the NA form of the dtype will cause it to
become NA as well.</p>
<p>When operations are done between arrays with NA dtypes and masked arrays,
the result will be masked arrays. This is because in some cases the
NA dtypes cannot represent all the values in the masked array, so
going to masked arrays is the only way to preserve all aspects of the data.</p>
<p>If np.NA or masked values are copied to an array without support for
missing values enabled, an exception will be raised. Adding a mask to
the target array would be problematic, because then having a mask
would be a “viral” property consuming extra memory and reducing
performance in unexpected ways.</p>
<p>By default, the string “NA” will be used to represent missing values
in str and repr outputs. A global configuration will allow
this to be changed, exactly extending the way nan and inf are treated.
The following works in the current draft implementation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([0, 1, 2, NA, 4, 5], maskna=True)</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">set_printoptions</span><span class="p">(</span><span class="n">nastr</span><span class="o">=</span><span class="s1">'blah'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([0, 1, 2, blah, 4, 5], maskna=True)</span>
</pre></div>
</div>
<p>For floating point numbers, Inf and NaN are separate concepts from
missing values. If a division by zero occurs in an array with default
missing value support, an unmasked Inf or NaN will be produced. To
mask those values, a further ‘a[np.logical_not(a.isfinite(a))] = np.NA’
can achieve that. For the bitpattern approach, the parameterized
dtype(‘NA[f8,InfNan]’) described in a later section can be used to get
these semantics without the extra manipulation.</p>
<p>A manual loop through a masked array like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">5.</span><span class="p">,</span> <span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([ 0., 1., 2., NA, 4.], maskna=True)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)):</span>
<span class="gp">... </span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="gp">...</span>
<span class="go">__main__:2: RuntimeWarning: divide by zero encountered in log</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([ -inf, 0. , 0.69314718, NA, 1.38629436], maskna=True)</span>
</pre></div>
</div>
<p>works even with masked values, because ‘a[i]’ returns an NA object
with a data type associated, that can be treated properly by the ufuncs.</p>
</section>
<section id="accessing-a-boolean-mask">
<h3><a class="toc-backref" href="#id13" role="doc-backlink">Accessing a boolean mask</a><a class="headerlink" href="#accessing-a-boolean-mask" title="Link to this heading">#</a></h3>
<p>The mask used to implement missing data in the masked approach is not
accessible from Python directly. This is partially due to differing
opinions on whether True in the mask should mean “missing” or “not missing”
Additionally, exposing the mask directly would preclude a potential
space optimization, where a bit-level instead of a byte-level mask
is used to get a factor of eight memory usage improvement.</p>
<p>To access a mask directly, there are two functions provided. They
work equivalently for both arrays with masks and NA bit
patterns, so they are specified in terms of NA and available values
instead of masked and unmasked values. The functions are
‘np.isna’ and ‘np.isavail’, which test for NA or available values
respectively.</p>
</section>
<section id="creating-na-masked-arrays">
<h3><a class="toc-backref" href="#id14" role="doc-backlink">Creating NA-masked arrays</a><a class="headerlink" href="#creating-na-masked-arrays" title="Link to this heading">#</a></h3>
<p>The usual way to create an array with an NA mask is to pass the keyword
parameter maskna=True to one of the constructors. Most functions that
create a new array take this parameter, and produce an NA-masked
array with all its elements exposed when the parameter is set to True.</p>
<p>There are also two flags which indicate and control the nature of the mask
used in masked arrays. These flags can be used to add a mask, or ensure
the mask isn’t a view into another array’s mask.</p>
<p>First is ‘arr.flags.maskna’, which is True for all masked arrays and
may be set to True to add a mask to an array which does not have one.</p>
<p>Second is ‘arr.flags.ownmaskna’, which is True if the array owns the
memory to the mask, and False if the array has no mask, or has a view
into the mask of another array. If this is set to True in a masked
array, the array will create a copy of the mask so that further modifications
to the mask will not affect the original mask from which the view was taken.</p>
</section>
<section id="na-masks-when-constructing-from-lists">
<h3><a class="toc-backref" href="#id15" role="doc-backlink">Na-masks when constructing from lists</a><a class="headerlink" href="#na-masks-when-constructing-from-lists" title="Link to this heading">#</a></h3>
<p>The initial design of NA-mask construction was to make all construction
fully explicit. This turns out to be unwieldy when working interactively
with NA-masked arrays, and having an object array be created instead of
an NA-masked array can be very surprising.</p>
<p>Because of this, the design has been changed to enable an NA-mask whenever
creating an array from lists which have an NA object in them. There could
be some debate of whether one should create NA-masks or NA-bitpatterns
by default, but due to the time constraints it was only feasible to tackle
NA-masks, and extending the NA-mask support more fully throughout NumPy seems
much more reasonable than starting another system and ending up with two
incomplete systems.</p>
</section>
<section id="mask-implementation-details">
<h3><a class="toc-backref" href="#id16" role="doc-backlink">Mask implementation details</a><a class="headerlink" href="#mask-implementation-details" title="Link to this heading">#</a></h3>
<p>The memory ordering of the mask will always match the ordering of
the array it is associated with. A Fortran-style array will have a
Fortran-style mask, etc.</p>
<p>When a view of an array with a mask is taken, the view will have
a mask which is also a view of the mask in the original
array. This means unmasking values in views will also unmask them
in the original array, and if a mask is added to an array, it will
not be possible to ever remove that mask except to create a new array
copying the data but not the mask.</p>
<p>It is still possible to temporarily treat an array with a mask without
giving it one, by first creating a view of the array and then adding a
mask to that view. A data set can be viewed with multiple different
masks simultaneously, by creating multiple views, and giving each view
a mask.</p>
</section>
<section id="new-ndarray-methods">
<h3><a class="toc-backref" href="#id17" role="doc-backlink">New ndarray methods</a><a class="headerlink" href="#new-ndarray-methods" title="Link to this heading">#</a></h3>
<p>New functions added to the numpy namespace are:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">isna</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="p">[</span><span class="n">IMPLEMENTED</span><span class="p">]</span>
<span class="n">Returns</span> <span class="n">a</span> <span class="n">boolean</span> <span class="n">array</span> <span class="k">with</span> <span class="kc">True</span> <span class="n">wherever</span> <span class="n">the</span> <span class="n">array</span> <span class="ow">is</span> <span class="n">masked</span>
<span class="ow">or</span> <span class="n">matches</span> <span class="n">the</span> <span class="n">NA</span> <span class="n">bitpattern</span><span class="p">,</span> <span class="ow">and</span> <span class="kc">False</span> <span class="n">elsewhere</span>
<span class="n">np</span><span class="o">.</span><span class="n">isavail</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="n">Returns</span> <span class="n">a</span> <span class="n">boolean</span> <span class="n">array</span> <span class="k">with</span> <span class="kc">False</span> <span class="n">wherever</span> <span class="n">the</span> <span class="n">array</span> <span class="ow">is</span> <span class="n">masked</span>
<span class="ow">or</span> <span class="n">matches</span> <span class="n">the</span> <span class="n">NA</span> <span class="n">bitpattern</span><span class="p">,</span> <span class="ow">and</span> <span class="kc">True</span> <span class="n">elsewhere</span>
</pre></div>
</div>
<p>New functions added to the ndarray are:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">arr</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">replacena</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">)</span>
<span class="n">Modification</span> <span class="n">to</span> <span class="n">the</span> <span class="n">copy</span> <span class="n">function</span> <span class="n">which</span> <span class="n">replaces</span> <span class="n">NA</span> <span class="n">values</span><span class="p">,</span>
<span class="n">either</span> <span class="n">masked</span> <span class="ow">or</span> <span class="k">with</span> <span class="n">the</span> <span class="n">NA</span> <span class="n">bitpattern</span><span class="p">,</span> <span class="k">with</span> <span class="n">the</span> <span class="s1">'replacena='</span>
<span class="n">parameter</span> <span class="n">supplied</span><span class="o">.</span> <span class="n">When</span> <span class="s1">'replacena'</span> <span class="n">isn</span><span class="s1">'t NA, the copied</span>
<span class="n">array</span> <span class="ow">is</span> <span class="n">unmasked</span> <span class="ow">and</span> <span class="n">has</span> <span class="n">the</span> <span class="s1">'NA'</span> <span class="n">part</span> <span class="n">stripped</span> <span class="kn">from</span><span class="w"> </span><span class="nn">the</span>
<span class="n">parameterized</span> <span class="n">dtype</span> <span class="p">(</span><span class="s1">'NA[f8]'</span> <span class="n">becomes</span> <span class="n">just</span> <span class="s1">'f8'</span><span class="p">)</span><span class="o">.</span>
<span class="n">The</span> <span class="n">default</span> <span class="k">for</span> <span class="n">replacena</span> <span class="ow">is</span> <span class="n">chosen</span> <span class="n">to</span> <span class="n">be</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span> <span class="n">instead</span> <span class="n">of</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">because</span> <span class="n">it</span> <span class="n">may</span> <span class="n">be</span> <span class="n">desirable</span> <span class="n">to</span> <span class="n">replace</span> <span class="n">NA</span> <span class="k">with</span> <span class="kc">None</span> <span class="ow">in</span> <span class="n">an</span>
<span class="n">NA</span><span class="o">-</span><span class="n">masked</span> <span class="nb">object</span> <span class="n">array</span><span class="o">.</span>
<span class="n">For</span> <span class="n">future</span> <span class="n">multi</span><span class="o">-</span><span class="n">NA</span> <span class="n">support</span><span class="p">,</span> <span class="s1">'replacena'</span> <span class="n">could</span> <span class="n">accept</span> <span class="n">a</span> <span class="n">dictionary</span>
<span class="n">mapping</span> <span class="n">the</span> <span class="n">NA</span> <span class="n">payload</span> <span class="n">to</span> <span class="n">the</span> <span class="n">value</span> <span class="n">to</span> <span class="n">substitute</span> <span class="k">for</span> <span class="n">that</span>
<span class="n">particular</span> <span class="n">NA</span><span class="o">.</span> <span class="n">NAs</span> <span class="k">with</span> <span class="n">payloads</span> <span class="ow">not</span> <span class="n">appearing</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">dictionary</span>
<span class="n">would</span> <span class="n">remain</span> <span class="k">as</span> <span class="n">NA</span> <span class="n">unless</span> <span class="n">a</span> <span class="s1">'default'</span> <span class="n">key</span> <span class="n">was</span> <span class="n">also</span> <span class="n">supplied</span><span class="o">.</span>
<span class="n">Both</span> <span class="n">the</span> <span class="n">parameter</span> <span class="n">to</span> <span class="n">replacena</span> <span class="ow">and</span> <span class="n">the</span> <span class="n">values</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">dictionaries</span>
<span class="n">can</span> <span class="n">be</span> <span class="n">either</span> <span class="n">scalars</span> <span class="ow">or</span> <span class="n">arrays</span> <span class="n">which</span> <span class="n">get</span> <span class="n">broadcast</span> <span class="n">onto</span> <span class="s1">'arr'</span><span class="o">.</span>
<span class="n">arr</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="p">[</span><span class="n">IMPLEMENTED</span><span class="p">]</span>
<span class="n">This</span> <span class="ow">is</span> <span class="n">a</span> <span class="n">shortcut</span> <span class="k">for</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">arr</span><span class="o">.</span><span class="n">view</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">a</span><span class="o">.</span><span class="n">flags</span><span class="o">.</span><span class="n">maskna</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">arr</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">ownmaskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="p">[</span><span class="n">IMPLEMENTED</span><span class="p">]</span>
<span class="n">This</span> <span class="ow">is</span> <span class="n">a</span> <span class="n">shortcut</span> <span class="k">for</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">arr</span><span class="o">.</span><span class="n">view</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">a</span><span class="o">.</span><span class="n">flags</span><span class="o">.</span><span class="n">maskna</span> <span class="o">=</span> <span class="kc">True</span>
<span class="o">>>></span> <span class="n">a</span><span class="o">.</span><span class="n">flags</span><span class="o">.</span><span class="n">ownmaskna</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
</section>
<section id="element-wise-ufuncs-with-missing-values">
<h3><a class="toc-backref" href="#id18" role="doc-backlink">Element-wise ufuncs with missing values</a><a class="headerlink" href="#element-wise-ufuncs-with-missing-values" title="Link to this heading">#</a></h3>
<p>As part of the implementation, ufuncs and other operations will
have to be extended to support masked computation. Because this
is a useful feature in general, even outside the context of
a masked array, in addition to working with masked arrays ufuncs
will take an optional ‘where=’ parameter which allows the use
of boolean arrays to choose where a computation should be done.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">b</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="p">(</span><span class="n">a</span> <span class="o">></span> <span class="n">threshold</span><span class="p">))</span>
</pre></div>
</div>
<p>A benefit of having this ‘where=’ parameter is that it provides a way
to temporarily treat an object with a mask without ever creating a
masked array object. In the example above, this would only do the
add for the array elements with True in the ‘where’ clause, and neither
‘a’ nor ‘b’ need to be masked arrays.</p>
<p>If the ‘out’ parameter isn’t specified, use of the ‘where=’ parameter
will produce an array with a mask as the result, with missing values
for everywhere the ‘where’ clause had the value False.</p>
<p>For boolean operations, the R project special cases logical_and and
logical_or so that logical_and(NA, False) is False, and
logical_or(NA, True) is True. On the other hand, 0 * NA isn’t 0, but
here the NA could represent Inf or NaN, in which case 0 * the backing
value wouldn’t be 0 anyway.</p>
<p>For NumPy element-wise ufuncs, the design won’t support this ability
for the mask of the output to depend simultaneously on the mask and
the value of the inputs. The NumPy 1.6 nditer, however, makes it
fairly easy to write standalone functions which look and feel just
like ufuncs, but deviate from their behavior. The functions logical_and
and logical_or can be moved into standalone function objects which are
backwards compatible with the current ufuncs.</p>
</section>
<section id="reduction-ufuncs-with-missing-values">
<h3><a class="toc-backref" href="#id19" role="doc-backlink">Reduction ufuncs with missing values</a><a class="headerlink" href="#reduction-ufuncs-with-missing-values" title="Link to this heading">#</a></h3>
<p>Reduction operations like ‘sum’, ‘prod’, ‘min’, and ‘max’ will operate
consistently with the idea that a masked value exists, but its value
is unknown.</p>
<p>An optional parameter ‘skipna=’ will be added to those functions
which can interpret it appropriately to do the operation as if just
the unmasked values existed.</p>
<p>With ‘skipna=True’, when all the input values are masked,
‘sum’ and ‘prod’ will produce the additive and multiplicative identities
respectively, while ‘min’ and ‘max’ will produce masked values.
Statistics operations which require a count, like ‘mean’ and ‘std’
will also use the unmasked value counts for their calculations if
‘skipna=True’, and produce masked values when all the inputs are masked.</p>
<p>Some examples:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="mf">7.</span><span class="p">],</span> <span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">array(NA, dtype='<f8', maskna=True)</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">11.0</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">NA(dtype='<f8')</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">3.6666666666666665</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NA</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'f8'</span><span class="p">,</span> <span class="n">maskna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>