bcolz
bcolz copied to clipboard
Problem with dtype in bcolz.carray constructor.
According to documentation, parameter dtype should "Force this dtype for the carray (rather than the array one)".
Let's consider following:
arr = np.arange(20).reshape((4, 5)) # dtype of array is np.int64
ds = bcolz.carray(arr, dtype=np.float32, expectedlen=arr.size)
And I got the TypeError:
bcolz/carray_ext.pyx in bcolz.carray_ext.carray.__cinit__ (bcolz/carray_ext.c:14817)()
bcolz/carray_ext.pyx in bcolz.carray_ext.carray._create_carray (bcolz/carray_ext.c:15599)()
/usr/local/lib/python2.7/site-packages/bcolz/utils.pyc in to_ndarray(array, dtype, arrlen, safe)
117 # Ensure that we have an ndarray of the correct dtype
118 if dtype is not None:
--> 119 if type(array) != np.ndarray or array.dtype != dtype.base:
120 try:
121 array = np.array(array, dtype=dtype.base)
TypeError: data type not understood
Yep. Perhaps forcing a view of the numpy array should be relatively easy to add. Interested in providing a PR? If not easy, perhaps fixing the documentation would be better.
I believe that simple
arr = arr.astype(dtype, copy=False)
can do the trick. At least there are no overheads. If so I'll try to provide PR.
@Kyrish
np.dtype('float32')
This might works, try.