Skip to content

[BUG] replace gives incomplete result on non-contiguous view #496

@pkomiske

Description

@pkomiske

Describe the bug
Calling bottleneck.replace on a non-contiguous view does not replace all occurrences of the old value. This leads to an incorrect result.

To Reproduce
This script reproduces the bug:

import bottleneck as bn
import numpy as np

print('Bottleneck version', bn.__version__)
print('Numpy version', np.__version__)
print()

# make array with some nans
x = np.random.rand(4, 3, 2)
x[::2,:,0] = np.nan

y = x[:,:,0]
print('Before:')
print(y)
print()

# attempt to replace nans
x2 = bn.replace(y, np.nan, 0)
print('After:')
print(x2)

which for me yields

Bottleneck version 1.6.0
Numpy version 2.0.2

Before:
[[       nan        nan        nan]
 [0.6648173  0.68779559 0.22735558]
 [       nan        nan        nan]
 [0.92285548 0.83657247 0.21408579]]

After:
[[0.         0.                nan]
 [0.6648173  0.68779559 0.22735558]
 [0.         0.                nan]
 [0.92285548 0.83657247 0.21408579]]

I would of course expect to see all the nans replaced, but the last one is not.

  • Python version is 3.12.6
  • OS is Ubuntu 22.04
  • pip version is 25.3
  • bottleneck and numpy versions are listed above

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions