apply_along_axis cuts strings
See original GitHub issueI’m trying to concatenate all elements of a row into a string as follows:
np.apply_along_axis(lambda x: " ".join(map(str, x)), 1, b)
b is
[[111,111,0,0,0], [111,111,111,111,111]]
However, the result of the line is:
['111 111 0 0 0', '111 111 111 1']
It looks like np.apply_along_axis is cutting the second string to be of the same length as the first one. If I put a longer sequence first, the result is correct:
['111 111 111 111 111', '111 111 0 0 0']
So I’m guessing this is a bug?
Summary 2019-04-30 by @seberg
np.apply_along_axis
infers the output dtype from the first pass. Which can be worked around for example but the function returning an array of a correct type.
Actions:
np.apply_along_axis
could/should get adtype
kwarg (or similar, compare alsonp.vectorize
).
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:16 (8 by maintainers)
Top Results From Across the Web
numpy.apply_along_axis truncates strings because it infers ...
I want to: apply_along_axis to my_array. for every row, return a string. def my_function(x): ...
Read more >numpy.apply_along_axis — NumPy v1.24 Manual
Apply a function to 1-D slices along the given axis. Execute func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is...
Read more >Python NumPy apply_along_axis() Function Example - Morioh
Python NumPy apply_along_axis() is an inbuilt NumPy function that is used to apply a function to 1D slices along the given axis of...
Read more >cupyx.GeneralizedUFunc — CuPy 12.0.0b2 documentation
signatures (list of tuple of str) – Contains strings in the form of 'ii->i' with i being the ... This is a short-cut...
Read more >Revisiting Numpy and ndarray - Towards Data Science
Let's have a closer look, U8 means string with 8 characters, ... very best of Towards Data Science: from hands-on tutorials and cutting-edge ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m assuming that that’s a deliberately contrived example, because you shouldn’t be using
apply_along_axis
for simple indexing like that.np.ma.apply_along_axis
will work correctly here (for now - see #8511). Another option (crashed until 1.13) is a manual cast todtype
object:Perhaps we should just add a
dtype=
argument toapply_along_axis