Skip to content

DOC: np.append docs should explain how appending different dtypes works #26291

Closed

Description

@Fleyderer

Describe the issue:

When trying to append empty list to existing array, numpy array changes its dtype. This is absolutely not obvious behaviour and I've barely found this problem.

It may sound like minor problem, but in my case it was a reason of not deleting about 50k images by its indices, when I had to.

Reproduce the code example:

a = np.array([1, 2], dtype=int)
b = []
np.append(a, b) # array([1., 2.])

Error message:

No response

Python and NumPy Versions:

Python 3.11.5
Numpy 1.26.4

Runtime Environment:

No response

Context for the issue:

No response

Activity

  1. rkern commented on Apr 16, 2024

    @rkern
    Member

    It's intended behavior, but it should be documented better.

  2. tuhinsharma121 commented on Apr 16, 2024

    @tuhinsharma121
    Contributor

    @rkern Can I work on a PR to document this behaviour? In that case can you assign this issue to me?

  3. Fleyderer commented on Apr 16, 2024

    @Fleyderer
    Author

    What is "intended" in changing dtype of array, which values are not changed¿

  4. rkern commented on Apr 16, 2024

    @rkern
    Member

    Both arguments are converted to ndarrays through the usual method of np.asarray(). The default dtype for empty lists is float64. Once the arguments are converted to ndarrays, then their dtypes are compared to figure out the common dtype they can both be safely coerced to; int64 and float64 can both go to float64.

    >>> np.asarray([]).dtype
    dtype('float64')

    If you want to work around this, even in the case of empty lists, be sure to coerce both of your arguments to ndarrays explicitly with dtype=int ahead of time.

    >>> a = np.array([1, 2], dtype=int)
    >>> b = np.array([], dtype=int)
    >>> np.append(a, b)
    array([1, 2])
  5. changed the title [-]BUG: np.append does change array dtype[/-] [+]DOC: np.append docs should explain how appending different dtypes works[/+] on Apr 16, 2024
  6. seberg commented on Apr 17, 2024

    @seberg
    Member

    For what it's worth, I would agree that for append casting to the first array would probably have been a nicer choice for NumPy, but that doesn't mean I think we should change it (at least not without a long warning which may be tedious).

    There was discussion (maybe even a PR?) to add a dtype= argument, which would allow you to write np.append(arr, ..., dtype=arr.dtype).
    np.concatenate already has that option and is really basically what np.append uses internally.
    That could be reactivated.

    (For the empty list case, the dtype= argument may still be awkward due to unsafe casting of the "inferred" float64.)


    Improving the docs is also good of course.

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