summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/arraysetops.py68
-rw-r--r--numpy/lib/nanfunctions.py4
-rw-r--r--numpy/lib/npyio.py5
-rw-r--r--numpy/lib/tests/test_arraysetops.py50
-rw-r--r--numpy/lib/tests/test_io.py15
-rw-r--r--numpy/lib/tests/test_nanfunctions.py25
6 files changed, 126 insertions, 41 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py
index 691550579..0755fffd1 100644
--- a/numpy/lib/arraysetops.py
+++ b/numpy/lib/arraysetops.py
@@ -90,7 +90,7 @@ def ediff1d(ary, to_end=None, to_begin=None):
return ed
-def unique(ar, return_index=False, return_inverse=False):
+def unique(ar, return_index=False, return_inverse=False, return_counts=False):
"""
Find the unique elements of an array.
@@ -109,6 +109,10 @@ def unique(ar, return_index=False, return_inverse=False):
return_inverse : bool, optional
If True, also return the indices of the unique array that can be used
to reconstruct `ar`.
+ return_counts : bool, optional
+ .. versionadded:: 1.9.0
+ If True, also return the number of times each unique value comes up
+ in `ar`.
Returns
-------
@@ -120,6 +124,10 @@ def unique(ar, return_index=False, return_inverse=False):
unique_inverse : ndarray, optional
The indices to reconstruct the (flattened) original array from the
unique array. Only provided if `return_inverse` is True.
+ unique_counts : ndarray, optional
+ .. versionadded:: 1.9.0
+ The number of times each of the unique values comes up in the
+ original array. Only provided if `return_counts` is True.
See Also
--------
@@ -162,41 +170,49 @@ def unique(ar, return_index=False, return_inverse=False):
try:
ar = ar.flatten()
except AttributeError:
- if not return_inverse and not return_index:
- return np.sort(list(set(ar)))
+ if not return_inverse and not return_index and not return_counts:
+ return np.sort(list((set(ar))))
else:
ar = np.asanyarray(ar).flatten()
+ optional_indices = return_index or return_inverse
+ optional_returns = optional_indices or return_counts
+
if ar.size == 0:
- if return_inverse and return_index:
- return ar, np.empty(0, np.bool), np.empty(0, np.bool)
- elif return_inverse or return_index:
- return ar, np.empty(0, np.bool)
+ if not optional_returns:
+ ret = ar
else:
- return ar
+ ret = (ar,)
+ if return_index:
+ ret += (np.empty(0, np.bool),)
+ if return_inverse:
+ ret += (np.empty(0, np.bool),)
+ if return_counts:
+ ret += (np.empty(0, np.intp),)
+ return ret
+
+ if optional_indices:
+ perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
+ aux = ar[perm]
+ else:
+ ar.sort()
+ aux = ar
+ flag = np.concatenate(([True], aux[1:] != aux[:-1]))
- if return_inverse or return_index:
+ if not optional_returns:
+ ret = aux[flag]
+ else:
+ ret = (aux[flag],)
if return_index:
- perm = ar.argsort(kind='mergesort')
- else:
- perm = ar.argsort()
- aux = ar[perm]
- flag = np.concatenate(([True], aux[1:] != aux[:-1]))
+ ret += (perm[flag],)
if return_inverse:
iflag = np.cumsum(flag) - 1
iperm = perm.argsort()
- if return_index:
- return aux[flag], perm[flag], iflag[iperm]
- else:
- return aux[flag], iflag[iperm]
- else:
- return aux[flag], perm[flag]
-
- else:
- ar.sort()
- flag = np.concatenate(([True], ar[1:] != ar[:-1]))
- return ar[flag]
-
+ ret += (np.take(iflag, iperm),)
+ if return_counts:
+ idx = np.concatenate(np.nonzero(flag) + ([ar.size],))
+ ret += (np.diff(idx),)
+ return ret
def intersect1d(ar1, ar2, assume_unique=False):
"""
diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py
index 5766084ab..badba32da 100644
--- a/numpy/lib/nanfunctions.py
+++ b/numpy/lib/nanfunctions.py
@@ -228,7 +228,7 @@ def nanmin(a, axis=None, out=None, keepdims=False):
# Check for all-NaN axis
mask = np.all(mask, axis=axis, keepdims=keepdims)
if np.any(mask):
- res = _copyto(res, mask, np.nan)
+ res = _copyto(res, np.nan, mask)
warnings.warn("All-NaN axis encountered", RuntimeWarning)
return res
@@ -327,7 +327,7 @@ def nanmax(a, axis=None, out=None, keepdims=False):
# Check for all-NaN axis
mask = np.all(mask, axis=axis, keepdims=keepdims)
if np.any(mask):
- res = _copyto(res, mask, np.nan)
+ res = _copyto(res, np.nan, mask)
warnings.warn("All-NaN axis encountered", RuntimeWarning)
return res
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index f69ca0c73..98b4b6e35 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -845,6 +845,11 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
continue
if usecols:
vals = [vals[i] for i in usecols]
+ if len(vals) != N:
+ line_num = i + skiprows + 1
+ raise ValueError("Wrong number of columns at line %d"
+ % line_num)
+
# Convert each value according to its column and store
items = [conv(val) for (conv, val) in zip(converters, vals)]
# Then pack it according to the dtype's nesting
diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py
index e44ccd12b..41d77c07f 100644
--- a/numpy/lib/tests/test_arraysetops.py
+++ b/numpy/lib/tests/test_arraysetops.py
@@ -14,31 +14,59 @@ class TestSetOps(TestCase):
def test_unique(self):
- def check_all(a, b, i1, i2, dt):
- msg = "check values failed for type '%s'" % dt
+ def check_all(a, b, i1, i2, c, dt):
+ base_msg = 'check {0} failed for type {1}'
+
+ msg = base_msg.format('values', dt)
v = unique(a)
assert_array_equal(v, b, msg)
- msg = "check indexes failed for type '%s'" % dt
- v, j = unique(a, 1, 0)
+ msg = base_msg.format('return_index', dt)
+ v, j = unique(a, 1, 0, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i1, msg)
- msg = "check reverse indexes failed for type '%s'" % dt
- v, j = unique(a, 0, 1)
+ msg = base_msg.format('return_inverse', dt)
+ v, j = unique(a, 0, 1, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i2, msg)
- msg = "check with all indexes failed for type '%s'" % dt
- v, j1, j2 = unique(a, 1, 1)
+ msg = base_msg.format('return_counts', dt)
+ v, j = unique(a, 0, 0, 1)
+ assert_array_equal(v, b, msg)
+ assert_array_equal(j, c, msg)
+
+ msg = base_msg.format('return_index and return_inverse', dt)
+ v, j1, j2 = unique(a, 1, 1, 0)
+ assert_array_equal(v, b, msg)
+ assert_array_equal(j1, i1, msg)
+ assert_array_equal(j2, i2, msg)
+
+ msg = base_msg.format('return_index and return_counts', dt)
+ v, j1, j2 = unique(a, 1, 0, 1)
+ assert_array_equal(v, b, msg)
+ assert_array_equal(j1, i1, msg)
+ assert_array_equal(j2, c, msg)
+
+ msg = base_msg.format('return_inverse and return_counts', dt)
+ v, j1, j2 = unique(a, 0, 1, 1)
+ assert_array_equal(v, b, msg)
+ assert_array_equal(j1, i2, msg)
+ assert_array_equal(j2, c, msg)
+
+ msg = base_msg.format(('return_index, return_inverse '
+ 'and return_counts'), dt)
+ v, j1, j2, j3 = unique(a, 1, 1, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, i2, msg)
+ assert_array_equal(j3, c, msg)
a = [5, 7, 1, 2, 1, 5, 7]*10
b = [1, 2, 5, 7]
i1 = [2, 3, 0, 1]
i2 = [2, 3, 0, 1, 0, 2, 3]*10
+ c = np.multiply([2, 1, 2, 2], 10)
# test for numeric arrays
types = []
@@ -49,7 +77,7 @@ class TestSetOps(TestCase):
for dt in types:
aa = np.array(a, dt)
bb = np.array(b, dt)
- check_all(aa, bb, i1, i2, dt)
+ check_all(aa, bb, i1, i2, c, dt)
# test for object arrays
dt = 'O'
@@ -57,13 +85,13 @@ class TestSetOps(TestCase):
aa[:] = a
bb = np.empty(len(b), dt)
bb[:] = b
- check_all(aa, bb, i1, i2, dt)
+ check_all(aa, bb, i1, i2, c, dt)
# test for structured arrays
dt = [('', 'i'), ('', 'i')]
aa = np.array(list(zip(a, a)), dt)
bb = np.array(list(zip(b, b)), dt)
- check_all(aa, bb, i1, i2, dt)
+ check_all(aa, bb, i1, i2, c, dt)
# test for ticket #2799
aa = [1.+0.j, 1- 1.j, 1]
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 418386e35..d0f81bde3 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -19,10 +19,13 @@ from numpy.lib._iotools import (ConverterError, ConverterLockError,
ConversionWarning)
from numpy.compat import asbytes, asbytes_nested, bytes, asstr
from nose import SkipTest
-from numpy.ma.testutils import (TestCase, assert_equal, assert_array_equal,
- assert_raises, run_module_suite)
+from numpy.ma.testutils import (
+ TestCase, assert_equal, assert_array_equal,
+ assert_raises, assert_raises_regex, run_module_suite
+)
from numpy.testing import assert_warns, assert_, build_err_msg
+
@contextlib.contextmanager
def tempdir(change_dir=False):
tmpdir = mkdtemp()
@@ -759,6 +762,14 @@ class TestLoadTxt(TestCase):
res = np.loadtxt(count())
assert_array_equal(res, np.arange(10))
+ def test_bad_line(self):
+ c = TextIO()
+ c.write('1 2 3\n4 5 6\n2 3')
+ c.seek(0)
+
+ # Check for exception and that exception contains line number
+ assert_raises_regex(ValueError, "3", np.loadtxt, c)
+
class Testfromregex(TestCase):
# np.fromregex expects files opened in binary mode.
diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py
index df332dfb1..f00aa0165 100644
--- a/numpy/lib/tests/test_nanfunctions.py
+++ b/numpy/lib/tests/test_nanfunctions.py
@@ -114,6 +114,31 @@ class TestNanFunctions_MinMax(TestCase):
assert_(res.shape == (3, 1))
res = f(mat)
assert_(np.isscalar(res))
+ # check that rows of nan are dealt with for subclasses (#4628)
+ mat[1] = np.nan
+ for f in self.nanfuncs:
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ res = f(mat, axis=0)
+ assert_(isinstance(res, np.matrix))
+ assert_(not np.any(np.isnan(res)))
+ assert_(len(w) == 0)
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ res = f(mat, axis=1)
+ assert_(isinstance(res, np.matrix))
+ assert_(np.isnan(res[1, 0]) and not np.isnan(res[0, 0])
+ and not np.isnan(res[2, 0]))
+ assert_(len(w) == 1, 'no warning raised')
+ assert_(issubclass(w[0].category, RuntimeWarning))
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ res = f(mat)
+ assert_(np.isscalar(res))
+ assert_(res != np.nan)
+ assert_(len(w) == 0)
class TestNanFunctions_ArgminArgmax(TestCase):