Skip to content

numpy.eye raises TypeError on numpy.uint64 input #9982

Closed

Description

@hameerabbasi

As title.

import numpy as np
I = np.eye(np.uint64(5))

Output: TypeError: slice indices must be integers or None or have an __index__ method on line 187 of numpy/lib/twodim_base.py (corresponds to line 188 of master branch)

If it helps, I'm using the latest Anaconda version on macOS High Sierra.

Activity

  1. charris commented on Nov 7, 2017

    @charris
    Member

    Curious

    In [6]: uint64(5).__index__()
    Out[6]: 5L
    

    The int64 type works fine, so it has something to do with unsigned. I think the error message is probably misleading.

  2. hameerabbasi commented on Nov 7, 2017

    @hameerabbasi
    ContributorAuthor

    From what I could tell, there was a subtraction, and a cast happening to do that safely which cast it to a floating-point datatype of some kind to be able to support the range. And the float didn't have an __index__ method.

  3. charris commented on Nov 7, 2017

    @charris
    Member

    Yes, that makes sense. I suppose the fix would be to convert the arguments to int up front. It is likely that this is not the only place where this problem occurs ...

  4. hameerabbasi commented on Nov 7, 2017

    @hameerabbasi
    ContributorAuthor

    That could be problematic in other cases where the argument is a huge array and copying it would require a lot of memory.

  5. seberg commented on Nov 7, 2017

    @seberg
    Member

    As a hackish solution it will work, since on all computers if you view it as int64 it will still be the correct numbers (assuming they are not too large). What I mean is using arr.view("int64")

  6. hameerabbasi commented on Nov 7, 2017

    @hameerabbasi
    ContributorAuthor

    In all cases where we are concerned, that should be fine. That big of an index (one that won't fit in int64 but will fit in uint64) is probably only possible in sparse arrays anyway.

    Edit: If we're looking for a bigger range, wouldn't arr.view('int') do the trick?

  7. seberg commented on Nov 7, 2017

    @seberg
    Member

    @hameerabbasi you can view the uint as the appropriate int as long as the uint is not too large. In general if you got a uint64 you have to use an int64 of course. Numpy has nothing larger then (u)int64.

    Generally, np.intp (which is an int64 on 64bit systems) is always large enough to hold a valid dimension/index into any array. But if you start of with any uint, of course you cannot in generally "view" it as an int without the risk of negative numbers appearing, and you always have to view it as int of the same size.

  8. seberg commented on Nov 7, 2017

    @seberg
    Member

    (int itself without an undscore, so not int_ in numpy is actually a C long which is 64bits on 64bit linux and otherwise 32bits, so especially for apps that run on windows and linux it is not even a reliable type at all)

  9. hameerabbasi commented on Nov 7, 2017

    @hameerabbasi
    ContributorAuthor

    I assumed that 'int' would be a normal Python 3 big integer, independent of platform. My mistake.

  10. seberg commented on Nov 7, 2017

    @seberg
    Member

    (sorry to spam the issue a bit). Well np.int on python3 is, but np.int is int and it is silly (it should rather not exist). If you do np.asarray(..., dtype=int) then it acts like I described.

  11. eric-wieser commented on Nov 13, 2017

    @eric-wieser
    Member

    An easy fix would be to do N, M = m.shape, which handles the conversion to int for us.

    Of course, we still need to convert k, which we could do with operator.index

  12. added a commit that references this issue on May 11, 2022
    425f2d2
  13. added 2 commits that reference this issue on May 11, 2022
    29a9c0c
    f935594
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions