numpy / numpy/numpy

numpy apply_along_axis drops values after casting incorrectly

Open
#5,193 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Hello,
I have found that when I make the following call using apply_along_axis and return an int rather than a float, the field with the incorrect type is cast to the right type and put into the array properly, but the value of the subsequent field is set to zero:

def f0(m,l,val):
    return np.apply_along_axis(f1,0,m,m,l,val)

def f1(v,m,l,val):
    return np.apply_along_axis(f2,0,m,v,l,val)

def f2(v1,v2,l,val):
    l[0] += 1
    if l[0] <= 2:
        return 1.
    if l[0] == 3:
        return val
    if l[0] == 4:
        return .34151
print f0(np.array(np.ones((1,2))),list([0]),0) <-- int
print f0(np.array(np.ones((1,2))),list([0]),0.) <-- float
print f0(np.array(np.ones((1,2))),list([0]),1) <-- int
print f0(np.array(np.ones((1,2))),list([0]),1.) <-- float
print type(f0(np.array(np.ones((1,2))),list([0]),1)[0][1])
print numpy.__version__

output:

[[ 1.  0.] <-- correctly zero
 [ 1.  0.]] <-- value dropped
[[ 1.       0.     ] <-- correctly zero
 [ 1.       0.34151]] <-- value correct
[[ 1.  1.] <-- correctly 1
 [ 1.  0.]] <-- value dropped
[[ 1.       1.     ] <-- correctly 1
 [ 1.       0.34151]] <-- value correct
<type 'numpy.float64'> <-- type is float64, this was correctly cast
1.8.2 <-- numpy version

discovered with @asna1005

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 by running the supplied f0/f1/f2 reproduction with NumPy 1.8.2 and compare the integer and floating-point cases. Trace the nested apply_along_axis calls to identify where the subsequent value is lost; done means the reproduced cases retain the expected value while preserving the demonstrated casting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, 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.