Using np.unique with the axis argument on a multidimensional array with zero in one or more of the dimensions hits ValueError failures, seemingly due to sizes/reshaping.
When axis is one of the zero dimensions (that is, shape[axis] == 0), it throws ValueError: cannot reshape array of size 0 into shape (0,newaxis).
When axis is a non-zero dimension (shape[axis] == 1), it throws ValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype.
Reproducing code example:
import numpy as np
arr = np.zeros(shape=(0, 10))
np.unique(arr, axis=0)
# ValueError: cannot reshape array of size 0 into shape (0,newaxis)
np.unique(arr, axis=1)
# ValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype
# equivalent code with non-zero dimension works as expected:
arr = np.zeros(shape=(1, 10))
np.unique(arr, axis=0)
# array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
np.unique(arr, axis=1)
# array([[0.]])
Error message:
np.unique(arr, axis=0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 6, in unique
File "~/.pyenv/versions/numpy-bug/lib/python3.6/site-packages/numpy/lib/arraysetops.py", line 275, in unique
ar = ar.reshape(orig_shape[0], -1)
ValueError: cannot reshape array of size 0 into shape (0,newaxis)
np.unique(arr, axis=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 6, in unique
File "~/.pyenv/versions/numpy-bug/lib/python3.6/site-packages/numpy/lib/arraysetops.py", line 280, in unique
consolidated = ar.view(dtype)
ValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype
Numpy/Python version information:
Numpy: 1.18.1
Python: 3.6.9 (default, Jul 10 2019, 12:25:55) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)]
Using
np.uniquewith theaxisargument on a multidimensional array with zero in one or more of the dimensions hitsValueErrorfailures, seemingly due to sizes/reshaping.When
axisis one of the zero dimensions (that is,shape[axis] == 0), it throwsValueError: cannot reshape array of size 0 into shape (0,newaxis).When
axisis a non-zero dimension (shape[axis] == 1), it throwsValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype.Reproducing code example:
Error message:
np.unique(arr, axis=0)np.unique(arr, axis=1)Numpy/Python version information:
Numpy:
1.18.1Python:
3.6.9 (default, Jul 10 2019, 12:25:55) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)]