8000 ENH: missingdata: Add maskna= parameter to np.linspace and np.logspace · numpy/numpy@ddc7e6e · GitHub
[go: up one dir, main page]

Skip to content

Commit ddc7e6e

Browse files
committed
ENH: missingdata: Add maskna= parameter to np.linspace and np.logspace
1 parent 0a22bfd commit ddc7e6e

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

numpy/core/function_base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numeric as _nx
44
from numeric import array
55

6-
def linspace(start, stop, num=50, endpoint=True, retstep=False):
6+
def linspace(start, stop, num=50, endpoint=True, retstep=False, maskna=False):
77
"""
88
Return evenly spaced numbers over a specified interval.
99
@@ -29,6 +29,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False):
2929
retstep : bool, optional
3030
If True, return (`samples`, `step`), where `step` is the spacing
3131
between samples.
32+
maskna : boolean
33+
If this is true, the returned array will have an NA mask.
3234
3335
Returns
3436
-------
@@ -73,22 +75,22 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False):
7375
"""
7476
num = int(num)
7577
if num <= 0:
76-
return array([], float)
78+
return array([], float, maskna=maskna)
7779
if endpoint:
7880
if num == 1:
79-
return array([float(start)])
81+
return array([float(start)], maskna=maskna)
8082
step = (stop-start)/float((num-1))
81-
y = _nx.arange(0, num) * step + start
83+
y = _nx.arange(0, num, maskna=maskna) * step + start
8284
y[-1] = stop
8385
else:
8486
step = (stop-start)/float(num)
85-
y = _nx.arange(0, num) * step + start
87+
y = _nx.arange(0, num, maskna=maskna) * step + start
8688
if retstep:
8789
return y, step
8890
else:
8991
return y
9092

91-
def logspace(start,stop,num=50,endpoint=True,base=10.0):
93+
def logspace(start,stop,num=50,endpoint=True,base=10.0, maskna=False):
9294
"""
9395
Return numbers spaced evenly on a log scale.
9496
@@ -114,6 +116,8 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0):
114116
The base of the log space. The step size between the elements in
115117
``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
116118
Default is 10.0.
119+
maskna : boolean
120+
If this is true, the returned array will have an NA mask.
117121
118122
Returns
119123
-------
@@ -162,6 +166,6 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0):
162166
>>> plt.show()
163167
164168
"""
165-
y = linspace(start,stop,num=num,endpoint=endpoint)
169+
y = linspace(start,stop,num=num,endpoint=endpoint,maskna=maskna)
166170
return _nx.power(base,y)
167171

numpy/core/tests/test_maskna.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,5 +1157,20 @@ def test_array_maskna_var_std():
11571157
res = np.std(a, axis=1, skipna=True)
11581158
assert_array_almost_equal(res, [1.0, 0.81649658092772603])
11591159

1160+
def test_array_maskna_linspace_logspace():
1161+
# np.linspace, np.logspace
1162+
1163+
a = np.linspace(2.0, 3.0, num=5)
1164+
b = np.linspace(2.0, 3.0, num=5, maskna=True)
1165+
assert_equal(a, b)
1166+
assert_(not a.flags.maskna)
1167+
assert_(b.flags.maskna)
1168+
1169+
a = np.logspace(2.0, 3.0, num=4)
1170+
b = np.logspace(2.0, 3.0, num=4, maskna=True)
1171+
assert_equal(a, b)
1172+
assert_(not a.flags.maskna)
1173+
assert_(b.flags.maskna)
1174+
11601175
if __name__ == "__main__":
11611176
run_module_suite()

0 commit comments

Comments
 (0)
0