You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
DOC: np.append docs should explain how appending different dtypes works #26291
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.])
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([]).dtypedtype('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])
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
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.)
Activity
rkern commentedon Apr 16, 2024
It's intended behavior, but it should be documented better.
tuhinsharma121 commentedon Apr 16, 2024
@rkern Can I work on a PR to document this behaviour? In that case can you assign this issue to me?
Fleyderer commentedon Apr 16, 2024
What is "intended" in changing dtype of array, which values are not changed¿
rkern commentedon Apr 16, 2024
Both arguments are converted to
ndarrays through the usual method ofnp.asarray(). The default dtype for empty lists isfloat64. Once the arguments are converted tondarrays, then their dtypes are compared to figure out the common dtype they can both be safely coerced to;int64andfloat64can both go tofloat64.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 withdtype=intahead of time.[-]BUG: np.append does change array dtype[/-][+]DOC: np.append docs should explain how appending different dtypes works[/+]seberg commentedon Apr 17, 2024
For what it's worth, I would agree that for
appendcasting 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 writenp.append(arr, ..., dtype=arr.dtype).np.concatenatealready has that option and is really basically whatnp.appenduses 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.