Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Fleyderer opened this issue Apr 16, 2024 · 5 comments · Fixed by #26303
Closed

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

Fleyderer opened this issue Apr 16, 2024 · 5 comments · Fixed by #26303

Comments

@Fleyderer
Copy link

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

@rkern
Copy link
Member

rkern commented Apr 16, 2024

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

@tuhinsharma121
Copy link
Contributor

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

@Fleyderer
Copy link
Author

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

@rkern
Copy link
Member

rkern commented Apr 16, 2024

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])

@ngoldbaum ngoldbaum changed the title BUG: np.append does change array dtype DOC: np.append docs should explain how appending different dtypes works Apr 16, 2024
@seberg
Copy link
Member

seberg commented Apr 17, 2024

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants