summaryrefslogtreecommitdiff
path: root/numpy/lib/twodim_base.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2011-08-27 21:46:08 -0600
committerCharles Harris <charlesr.harris@gmail.com>2011-08-27 21:46:08 -0600
commit9ecd91b7bf8c77d696ec9856ba10896d8f60309a (patch)
tree9884131ece5eada06212538c591965bf5928afa2 /numpy/lib/twodim_base.py
parentaa55ba7437fbe6b8772a360a641b5aa7d3e669e0 (diff)
parent10fac981763e87f949bed15c66127fc380fa9b27 (diff)
downloadnumpy-9ecd91b7bf8c77d696ec9856ba10896d8f60309a.tar.gz
Merge branch 'pull-141'
* pull-141: (167 commits) ENH: missingdata: Make PyArray_Converter and PyArray_OutputConverter safer for legacy code DOC: missingdata: Add a mention of the design NEP, and masks vs bitpatterns DOC: missingdata: Updates from pull request feedback DOC: missingdata: Updates based on pull request feedback ENH: nditer: Change the Python nditer exposure to automatically add NPY_ITER_USE_MASKNA ENH: missingdata: Make comparisons with NA return NA(dtype='bool') BLD: core: onefile build fix and Python3 compatibility change DOC: Mention the update to np.all and np.any in the release notes TST: dtype: Adjust void dtype test to pass without raising a zero-size exception STY: Remove trailing whitespace TST: missingdata: Write some tests for the np.any and np.all NA behavior ENH: missingdata: Make numpy.all follow the NA && False == False rule ENH: missingdata: Make numpy.all follow the NA || True == True rule DOC: missingdata: Also show what assigning a non-NA value does in each case DOC: missingdata: Add introductory documentation for NA-masked arrays ENH: core: Rename PyArrayObject_fieldaccess to PyArrayObject_fields DOC: missingdata: Some tweaks to the NA mask documentation DOC: missingdata: Add example of a C-API function supporting NA masks DOC: missingdata: Documenting C API for NA-masked arrays ENH: missingdata: Finish adding C-API access to the NpyNA object ...
Diffstat (limited to 'numpy/lib/twodim_base.py')
-rw-r--r--numpy/lib/twodim_base.py31
1 files changed, 10 insertions, 21 deletions
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index d95a59e3f..12bba99a6 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -9,7 +9,7 @@ __all__ = ['diag','diagflat','eye','fliplr','flipud','rot90','tri','triu',
from numpy.core.numeric import asanyarray, equal, subtract, arange, \
zeros, greater_equal, multiply, ones, asarray, alltrue, where, \
- empty
+ empty, diagonal
def fliplr(m):
"""
@@ -166,7 +166,7 @@ def rot90(m, k=1):
# k == 3
return fliplr(m.swapaxes(0,1))
-def eye(N, M=None, k=0, dtype=float):
+def eye(N, M=None, k=0, dtype=float, maskna=False):
"""
Return a 2-D array with ones on the diagonal and zeros elsewhere.
@@ -182,6 +182,8 @@ def eye(N, M=None, k=0, dtype=float):
to a lower diagonal.
dtype : data-type, optional
Data-type of the returned array.
+ maskna : boolean
+ If this is true, the returned array will have an NA mask.
Returns
-------
@@ -207,24 +209,20 @@ def eye(N, M=None, k=0, dtype=float):
"""
if M is None:
M = N
- m = zeros((N, M), dtype=dtype)
- if k >= M:
- return m
- if k >= 0:
- i = k
- else:
- i = (-k) * M
- m[:M-k].flat[i::M+1] = 1
+ m = zeros((N, M), dtype=dtype, maskna=maskna)
+ diagonal(m, k)[...] = 1
return m
def diag(v, k=0):
"""
Extract a diagonal or construct a diagonal array.
+ As of NumPy 1.7, extracting a diagonal always returns a view into `v`.
+
Parameters
----------
v : array_like
- If `v` is a 2-D array, return a copy of its `k`-th diagonal.
+ If `v` is a 2-D array, return a view of its `k`-th diagonal.
If `v` is a 1-D array, return a 2-D array with `v` on the `k`-th
diagonal.
k : int, optional
@@ -278,16 +276,7 @@ def diag(v, k=0):
res[:n-k].flat[i::n+1] = v
return res
elif len(s) == 2:
- if k >= s[1]:
- return empty(0, dtype=v.dtype)
- if v.flags.f_contiguous:
- # faster slicing
- v, k, s = v.T, -k, s[::-1]
- if k >= 0:
- i = k
- else:
- i = (-k) * s[1]
- return v[:s[1]-k].flat[i::s[1]+1]
+ return v.diagonal(k)
else:
raise ValueError("Input must be 1- or 2-d.")