8000 Merge pull request #25414 from linus-md/ma-2 · numpy/numpy@7b0c33e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b0c33e

Browse files
authored
Merge pull request #25414 from linus-md/ma-2
DOC: Add missing examples to ``np.ma``
2 parents fc1bc54 + b417c6d commit 7b0c33e

File tree

2 files changed

+139
-33
lines changed

2 files changed

+139
-33
lines changed

numpy/ma/core.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,10 @@ def filled(a, fill_value=None):
633633
634634
Examples
635635
--------
636-
>>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
637-
... [1, 0, 0],
638-
... [0, 0, 0]])
636+
>>> import numpy.ma as ma
637+
>>> x = ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
638+
... [1, 0, 0],
639+
... [0, 0, 0]])
639640
>>> x.filled()
640641
array([[999999, 1, 2],
641642
[999999, 4, 5],
@@ -5541,6 +5542,17 @@ def round(self, decimals=0, out=None):
55415542
--------
55425543
numpy.ndarray.round : corresponding function for ndarrays
55435544
numpy.around : equivalent function
5545+
5546+
Examples
5547+
--------
5548+
>>> import numpy.ma as ma
5549+
>>> x = ma.array([1.35, 2.5, 1.5, 1.75, 2.25, 2.75],
5550+
... mask=[0, 0, 0, 1, 0, 0])
5551+
>>> ma.round(x)
5552+
masked_array(data=[1.0, 2.0, 2.0, --, 2.0, 3.0],
5553+
mask=[False, False, False, True, False, False],
5554+
fill_value=1e+20)
5555+
55445556
"""
55455557
result = self._data.round(decimals=decimals, out=out).view(type(self))
55465558
if result.ndim > 0:

numpy/ma/extras.py

Lines changed: 124 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ def count_masked(arr, axis=None):
6969
7070
Examples
7171
--------
72-
>>> import numpy.ma as ma
7372
>>> a = np.arange(9).reshape((3,3))
74-
>>> a = ma.array(a)
75-
>>> a[1, 0] = ma.masked
76-
>>> a[1, 2] = ma.masked
77-
>>> a[2, 1] = ma.masked
73+
>>> a = np.ma.array(a)
74+
>>> a[1, 0] = np.ma.masked
75+
>>> a[1, 2] = np.ma.masked
76+
>>> a[2, 1] = np.ma.masked
7877
>>> a
7978
masked_array(
8079
data=[[0, 1, 2],
@@ -84,14 +83,14 @@ def count_masked(arr, axis=None):
8483
[ True, False, True],
8584
[False, True, False]],
8685
fill_value=999999)
87-
>>> ma.count_masked(a)
86+
>>> np.ma.count_masked(a)
8887
3
8988
9089
When the `axis` keyword is used an array is returned.
9190
92-
>>> ma.count_masked(a, axis=0)
91+
>>> np.ma.count_masked(a, axis=0)
9392
array([1, 1, 1])
94-
>>> ma.count_masked(a, axis=1)
93+
>>> np.ma.count_masked(a, axis=1)
9594
array([0, 2, 1])
9695
9796
"""
@@ -124,8 +123,7 @@ def masked_all(shape, dtype=float):
124123
125124
Examples
126125
--------
127-
>>> import numpy.ma as ma
128-
>>> ma.masked_all((3, 3))
126+
>>> np.ma.masked_all((3, 3))
129127
masked_array(
130128
data=[[--, --, --],
131129
[--, --, --],
@@ -138,10 +136,10 @@ def masked_all(shape, dtype=float):
138136
139137
The `dtype` parameter defines the underlying data type.
140138
141-
>>> a = ma.masked_all((3, 3))
139+
>>> a = np.ma.masked_all((3, 3))
142140
>>> a.dtype
143141
dtype('float64')
144-
>>> a = ma.masked_all((3, 3), dtype=np.int32)
142+
>>> a = np.ma.masked_all((3, 3), dtype=np.int32)
145143
>>> a.dtype
146144
dtype('int32')
147145
@@ -179,12 +177,11 @@ def masked_all_like(arr):
179177
180178
Examples
181179
--------
182-
>>> import numpy.ma as ma
183180
>>> arr = np.zeros((2, 3), dtype=np.float32)
184181
>>> arr
185182
array([[0., 0., 0.],
186183
[0., 0., 0.]], dtype=float32)
187-
>>> ma.masked_all_like(arr)
184+
>>> np.ma.masked_all_like(arr)
188185
masked_array(
189186
data=[[--, --, --],
190187
[--, --, --]],
@@ -197,7 +194,7 @@ def masked_all_like(arr):
197194
198195
>>> arr.dtype
199196
dtype('float32')
200-
>>> ma.masked_all_like(arr).dtype
197+
>>> np.ma.masked_all_like(arr).dtype F438
201198
dtype('float32')
202199
203200
"""
@@ -839,6 +836,15 @@ def compress_nd(x, axis=None):
839836
-------
840837
compress_array : ndarray
841838
The compressed array.
839+
840+
Examples
841+
--------
842+
>>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
843+
... [1, 0, 0],
844+
... [0, 0, 0]])
845+
>>> np.ma.compress_nd(x)
846+
array([[7, 8]])
847+
842848
"""
843849
x = asarray(x)
844850
m = getmask(x)
@@ -928,6 +934,14 @@ def compress_rows(a):
928934
--------
929935
compress_rowcols
930936
937+
Examples
938+
--------
939+
>>> a = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
940+
... [1, 0, 0],
941+
... [0, 0, 0]])
942+
>>> np.ma.compress_rows(a)
943+
array([[6, 7, 8]])
944+
931945
"""
932946
a = asarray(a)
933947
if a.ndim != 2:
@@ -946,6 +960,16 @@ def compress_cols(a):
946960
--------
947961
compress_rowcols
948962
963+
Examples
964+
--------
965+
>>> a = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
966+
... [1, 0, 0],
967+
... [0, 0, 0]])
968+
>>> np.ma.compress_cols(a)
969+
array([[1, 2],
970+
[4, 5],
971+
[7, 8]])
972+
949973
"""
950974
a = asarray(a)
951975
if a.ndim != 2:
@@ -998,14 +1022,13 @@ def mask_rowcols(a, axis=None):
9981022
9991023
Examples
10001024
--------
1001-
>>> import numpy.ma as ma
10021025
>>> a = np.zeros((3, 3), dtype=int)
10031026
>>> a[1, 1] = 1
10041027
>>> a
10051028
array([[0, 0, 0],
10061029
[0, 1, 0],
10071030
[0, 0, 0]])
1008-
>>> a = ma.masked_equal(a, 1)
1031+
>>> a = np.ma.masked_equal(a, 1)
10091032
>>> a
10101033
masked_array(
10111034
data=[[0, 0, 0],
@@ -1015,7 +1038,7 @@ def mask_rowcols(a, axis=None):
10151038
[False, True, False],
10161039
[False, False, False]],
10171040
fill_value=1)
1018-
>>> ma.mask_rowcols(a)
1041+
>>> np.ma.mask_rowcols(a)
10191042
masked_array(
10201043
data=[[0, --, 0],
10211044
[--, --, --],
@@ -1055,14 +1078,13 @@ def mask_rows(a, axis=np._NoValue):
10551078
10561079
Examples
10571080
--------
1058-
>>> import numpy.ma as ma
10591081
>>> a = np.zeros((3, 3), dtype=int)
10601082
>>> a[1, 1] = 1
10611083
>>> a
10621084
array([[0, 0, 0],
10631085
[0, 1, 0],
10641086
[0, 0, 0]])
1065-
>>> a = ma.masked_equal(a, 1)
1087+
>>> a = np.ma.masked_equal(a, 1)
10661088
>>> a
10671089
masked_array(
10681090
data=[[0, 0, 0],
@@ -1073,7 +1095,7 @@ def mask_rows(a, axis=np._NoValue):
10731095
[False, False, False]],
10741096
fill_value=1)
10751097
1076-
>>> ma.mask_rows(a)
1098+
>>> np.ma.mask_rows(a)
10771099
masked_array(
10781100
data=[[0, 0, 0],
10791101
[--, --, --],
@@ -1106,14 +1128,13 @@ def mask_cols(a, axis=np._NoValue):
11061128
11071129
Examples
11081130
--------
1109-
>>> import numpy.ma as ma
11101131
>>> a = np.zeros((3, 3), dtype=int)
11111132
>>> a[1, 1] = 1
11121133
>>> a
11131134
array([[0, 0, 0],
11141135
[0, 1, 0],
11151136
[0, 0, 0]])
1116-
>>> a = ma.masked_equal(a, 1)
1137+
>>> a = np.ma.masked_equal(a, 1)
11171138
>>> a
11181139
masked_array(
11191140
data=[[0, 0, 0],
@@ -1123,7 +1144,7 @@ def mask_cols(a, axis=np._NoValue):
11231144
[False, True, False],
11241145
[False, False, False]],
11251146
fill_value=1)
1126-
>>> ma.mask_cols(a)
1147+
>>> np.ma.mask_cols(a)
11271148
masked_array(
11281149
data=[[0, --, 0],
11291150
[0, --, 0],
@@ -1158,6 +1179,14 @@ def ediff1d(arr, to_end=None, to_begin=None):
11581179
--------
11591180
numpy.ediff1d : Equivalent function for ndarrays.
11601181
1182+
Examples
1183+
--------
1184+
>>> arr = np.ma.array([1, 2, 4, 7, 0])
1185+
>>> np.ma.ediff1d(arr)
1186+
masked_array(data=[ 1, 2, 3, -7],
1187+
mask=False,
1188+
fill_value=999999)
1189+
F438
11611190
"""
11621191
arr = ma.asanyarray(arr).flat
11631192
ed = arr[1:] - arr[:-1]
@@ -1189,27 +1218,26 @@ def unique(ar1, return_index=False, return_inverse=False):
11891218
11901219
Examples
11911220
--------
1192-
>>> import numpy.ma as ma
11931221
>>> a = [1, 2, 1000, 2, 3]
11941222
>>> mask = [0, 0, 1, 0, 0]
1195-
>>> masked_a = ma.masked_array(a, mask)
1223+
>>> masked_a = np.ma.masked_array(a, mask)
11961224
>>> masked_a
11971225
masked_array(data=[1, 2, --, 2, 3],
11981226
mask=[False, False, True, False, False],
11991227
fill_value=999999)
1200-
>>> ma.unique(masked_a)
1228+
>>> np.ma.unique(masked_a)
12011229
masked_array(data=[1, 2, 3, --],
12021230
mask=[False, False, False, True],
12031231
fill_value=999999)
1204-
>>> ma.unique(masked_a, return_index=True)
1232+
>>> np.ma.unique(masked_a, return_index=True)
12051233
(masked_array(data=[1, 2, 3, --],
12061234
mask=[False, False, False, True],
12071235
fill_value=999999), array([0, 1, 4, 2]))
1208-
>>> ma.unique(masked_a, return_inverse=True)
1236+
>>> np.ma.unique(masked_a, return_inverse=True)
12091237
(masked_array(data=[1, 2, 3, --],
12101238
mask=[False, False, False, True],
12111239
fill_value=999999), array([0, 1, 3, 1, 2]))
1212-
>>> ma.unique(masked_a, return_index=True, return_inverse=True)
1240+
>>> np.ma.unique(masked_a, return_index=True, return_inverse=True)
12131241
(masked_array(data=[1, 2, 3, --],
12141242
mask=[False, False, False, True],
12151243
fill_value=999999), array([0, 1, 4, 2]), array([0, 1, 3, 1, 2]))
@@ -1268,6 +1296,15 @@ def setxor1d(ar1, ar2, assume_unique=False):
12681296
--------
12691297
numpy.setxor1d : Equivalent function for ndarrays.
12701298
1299+
Examples
1300+
--------
1301+
>>> ar1 = np.ma.array([1, 2, 3, 2, 4])
1302+
>>> ar2 = np.ma.array([2, 3, 5, 7, 5])
1303+
>>> np.ma.setxor1d(ar1, ar2)
1304+
masked_array(data=[1, 4, 5, 7],
1305+
mask=False,
1306+
fill_value=999999)
1307+
12711308
"""
12721309
if not assume_unique:
12731310
ar1 = unique(ar1)
@@ -1303,6 +1340,15 @@ def in1d(ar1, ar2, assume_unique=False, invert=False):
13031340
-----
13041341
.. versionadded:: 1.4.0
13051342
1343+
Examples
1344+
--------
1345+
>>> ar1 = np.ma.array([0, 1, 2, 5, 0])
1346+
>>> ar2 = [0, 2]
1347+
>>> np.ma.in1d(ar1, ar2)
1348+
masked_array(data=[ True, False, True, False, True],
1349+
mask=False,
1350+
fill_value=True)
1351+
13061352
"""
13071353
if not assume_unique:
13081354
ar1, rev_idx = unique(ar1, return_inverse=True)
@@ -1344,6 +1390,15 @@ def isin(element, test_elements, assume_unique=False, invert=False):
13441390
-----
13451391
.. versionadded:: 1.13.0
13461392
1393+
Examples
1394+
--------
1395+
>>> element = np.ma.array([1, 2, 3, 4, 5, 6])
1396+
>>> test_elements = [0, 2]
1397+
>>> np.ma.isin(element, test_elements)
1398+
masked_array(data=[False, True, False, False, False, False],
1399+
mask=False,
1400+
fill_value=True)
1401+
13471402
"""
13481403
element = ma.asarray(element)
13491404
return in1d(element, test_elements, assume_unique=assume_unique,
@@ -1360,6 +1415,15 @@ def union1d(ar1, ar2):
13601415
--------
13611416
numpy.union1d : Equivalent function for ndarrays.
13621417
1418+
Examples
1419+
--------
1420+
>>> ar1 = np.ma.array([1, 2, 3, 4])
1421+
>>> ar2 = np.ma.array([3, 4, 5, 6])
1422+
>>> np.ma.union1d(ar1, ar2)
1423+
masked_array(data=[1, 2, 3, 4, 5, 6],
1424+
mask=False,
1425+
fill_value=999999)
1426+
13631427
"""
13641428
return unique(ma.concatenate((ar1, ar2), axis=None))
13651429

@@ -1492,6 +1556,23 @@ def cov(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None):
14921556
--------
14931557
numpy.cov
14941558
1559+
Examples
1560+
--------
1561+
>>> x = np.ma.array([[0, 1], [1, 1]], mask=[0, 1, 0, 1])
1562+
>>> y = np.ma.array([[1, 0], [0, 1]], mask=[0, 0, 1, 1])
1563+
>>> np.ma.cov(x, y)
1564+
masked_array(
1565+
data=[[--, --, --, --],
1566+
[--, --, --, --],
1567+
[--, --, --, --],
1568+
[--, --, --, --]],
1569+
mask=[[ True, True, True, True],
1570+
[ True, True, True, True],
1571+
[ True, True, True, True],
1572+
[ True, True, True, True]],
1573+
fill_value=1e+20,
1574+
dtype=float64)
1575+
14951576
"""
14961577
# Check inputs
14971578
if ddof is not None and ddof != int(ddof):
@@ -1560,6 +1641,19 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, allow_masked=True,
15601641
for backwards compatibility with previous versions of this function. These
15611642
arguments had no effect on the return values of the function and can be
15621643
safely ignored in this and previous versions of numpy.
1644+
1645+
Examples
1646+
--------
1647+
>>> x = np.ma.array([[0, 1], [1, 1]], mask=[0, 1, 0, 1])
1648+
>>> np.ma.corrcoef(x)
1649+
masked_array(
1650+
data=[[--, --],
1651+
[--, --]],
1652+
mask=[[ True, True],
1653+
[ True, True]],
1654+
fill_value=1e+20,
1655+
dtype=float64)
1656+
15631657
"""
15641658
msg = 'bias and ddof have no effect and are deprecated'
15651659
if bias is not np._NoValue or ddof is not np._NoValue:

0 commit comments

Comments
 (0)
0