Skip to content

bincount does not accept input of type > N.uint16 (Trac #225) #823

Closed

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/225 on 2006-08-03 by @stefanv, assigned to @teoliphant.

Under r2944:

In [22]: N.bincount(N.array([1],N.uint16))
Out[22]: array([0, 1])





In [23]: N.bincount(N.array([1],N.uint32))
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/home/stefan/work/scipy/hough/<ipython console>

TypeError: array cannot be safely cast to required type

Activity

  1. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    @stefanv wrote on 2006-08-03

    Can be improved by changing to NPY_UINTP, but that still won't allow 64-bit integers. Is there a way to easily fix this for 32-bit systems?

  2. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Milestone changed to 1.1 by @alberts on 2007-05-12

  3. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Milestone changed to Unscheduled by @cournape on 2009-03-02

  4. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    @mwiebe wrote on 2011-03-23

    This function still errors (with uint64 on a 64-bit machine). Bincount is a candidate to convert to using the iterator with buffering, since currently it will cause a copy if the input isn't contiguous and the right type.

  5. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    @bsouthey wrote on 2011-05-09

    My C is not very good but hopefully someone with can correct the patch. The less obvious change (at least was to me) was that PyArray_ContiguousFromAny was being called with unsigned int, PyArray_INTP, rather than PyArray_UINTP.

    According to the comment on the bincount code in "numpy/lib/src/_compiled_base.c", the first argument must be an array of non-negative integers (relevant lines):

     * bincount accepts one, two or three arguments. The first is an array of
     * non-negative integers The second, if present, is an array of weights,
    

    Consequently I changed bincount to use unsigned ints instead of signed ints.

    The patch is incorrect if the mxx and mnx functions can be used outside of bincount. In that case new functions would have to declared.

  6. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Attachment added by @bsouthey on 2011-05-09: 0001-bincount-unsigned-ints.patch

  7. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Attachment added by @bsouthey on 2011-05-09: 0002-Redo-bincount-signed-int-change.patch

  8. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    @bsouthey wrote on 2011-05-09

    Okay, still was not that simple! I had to change one PyObject variable into PyArrayObject. This allowed to get the input dtype for PyArray?_ContiguousFromAny. Both patches need to be applied because I do not know how to get a single patch with git without redoing everything - sorry!

  9. OmerJog commented on Feb 6, 2019

    @OmerJog

    Is this still an issue?

  10. endolith commented on Apr 17, 2019

    @endolith
    Contributor

    @OmerJog The original example doesn't fail:

    np.bincount(np.array([1], np.uint32))
    Out[1]: array([0, 1], dtype=int64)
    

    but this does:

    np.bincount(np.array([1], np.uint64))
    Traceback (most recent call last):
    
      File "<ipython-input-2-850f8c15a097>", line 1, in <module>
        np.bincount(np.array([1], np.uint64))
    
    TypeError: Cannot cast array data from dtype('uint64') to dtype('int64') according to the rule 'safe'
    
  11. added
    57 - Close?Issues which may be closable unless discussion continued
    on Aug 4, 2026
  12. eendebakpt commented on Aug 4, 2026

    @eendebakpt
    Contributor

    I cannot reproduce on current numpy.

  13. mattip commented on Sep 7, 2026

    @mattip
    Member

    Closing as fixed.

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