Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:30:10+00:00 2026-06-18T12:30:10+00:00

I have a look-up table (LUT) that stores 65536 uint8 values: lut = np.random.randint(256,

  • 0

I have a look-up table (LUT) that stores 65536 uint8 values:

lut = np.random.randint(256, size=(65536,)).astype('uint8')

I want to use this LUT to convert the values in an array of uint16s:

arr = np.random.randint(65536, size=(1000, 1000)).astype('uint16')

and I want to do the conversion in place, because this last array can get pretty big. When I try it, the following happens:

>>> np.take(lut, arr, out=arr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 103, in take
    return take(indices, axis, out, mode)
TypeError: array cannot be safely cast to required type

And I don’t understand what is going on. I know that, without an out argument, the return is of the same dtype as lut, so uint8. But why can’t a uint8 be cast to a uint16? If you ask numpy:

>>> np.can_cast('uint8', 'uint16')
True

Obviously the following works:

>>> lut = lut.astype('uint16')
>>> np.take(lut, arr, out=arr)
array([[173, 251, 218, ..., 110,  98, 235],
       [200, 231,  91, ..., 158, 100,  88],
       [ 13, 227, 223, ...,  94,  56,  36],
       ..., 
       [ 28, 198,  80, ...,  60,  87, 118],
       [156,  46, 118, ..., 212, 198, 218],
       [203,  97, 245, ...,   3, 191, 173]], dtype=uint16)

But this also works:

>>> lut = lut.astype('int32')
>>> np.take(lut, arr, out=arr)
array([[ 78, 249, 148, ...,  77,  12, 167],
       [138,   5, 206, ...,  31,  43, 244],
       [ 29, 134, 131, ..., 100, 107,   1],
       ..., 
       [109, 166,  14, ...,  64,  95, 102],
       [152, 169, 102, ..., 240, 166, 148],
       [ 47,  14, 129, ..., 237,  11,  78]], dtype=uint16)

This really makes no sense, since now int32s are being cast to uint16s, which is definitely not a safe thing to do:

>>> np.can_cast('int32', 'uint16')
False

My code works if I set the lut‘s dtype to anything in uint16, uint32, uint64, int32 or int64, but fails for uint8, int8 and int16.

Am I missing something, or is this simply broken in numpy?

Workarounds are also welcome… Since the LUT is not that big, I guess it is not that bad to have its type match the array’s, even if that takes twice the space, but it just doesn’t feel right to do that…

Is there a way to tell numpy to not worry about casting safety?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:30:12+00:00Added an answer on June 18, 2026 at 12:30 pm

    Interesting problem. numpy.take(lut, ...) gets transformed into lut.take(...) whose source can be looked at here:

    https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/item_selection.c#L28

    I believe the exception is thrown at line 105:

    obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags);
    if (obj == NULL) {
        goto fail;
    }
    

    where in your case out is arr but dtype is the one of lut, i.e. uint8. So it tries to cast arr to uint8, which fails. I have to say that I’m not sure why it needs to do that, just pointing out it does… For some reason take seems to assume you want as the output array to have the same dtype as lut.

    By the way, in many cases the call to PyArray_FromArray will actually create a new array and the replacement will not be in place. This is the case for example if you call take with mode='raise' (the default, and what happens in your examples), or whenever lut.dtype != arr.dtype. Well, at least it should, and I can’t explain why, when you cast lut to int32 the output array remains uint16! This is a mystery to me – maybe it has something to do with the NPY_ARRAY_UPDATEIFCOPY flag (see also here).

    Bottom line:

    1. the behavior of numpy is indeed difficult to understand… Maybe someone else will provide some insight into why it does what it does
    2. I would not try to process arr in place – it seems that a new array is created under the hood in most cases anyway. I’d simply go with arr = lut.take(arr) – which by the way will eventually free half of the memory previously used by arr.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with contents that look similar to this: id | title
I have a table posts that could look like this: id | title |
I have a table of products sales that may look as follows: product |
I have multiple buttons that gets created in a table which all look like
On my pages I have a table with rows that typically look like this:
I have a lookup table (LUT) of thousands integers that I use on a
I have a 'look-up' table with pre-set values (was informed it's better than ENUM)
I have look-up-table as defined below and I'm making use of GCC. When I
I have a couple of tables which look like this Table 1 user_id |
I have to create one table using XSLT and CSS. The table should look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.