numpy / numpy/numpy

apply_along_axis cuts strings

Open
#8,352 16 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

01 - Enhancement component: numpy.lib defunct — difficulty: Intermediate
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

I'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 a dtype kwarg (or similar, compare also np.vectorize).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the np.apply_along_axis entry point and reproduce the supplied example, comparing results when the longer row comes first or second. Determine how the first pass sets the output dtype; done means the example preserves both complete strings, with the chosen dtype-handling behavior reflected in the API.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.