NumPy's np.roll not behaving as expected when used with masked arrays
I've been banging my head against this for hours..... I'm using NumPy version 1.23.2 and I've encountered a puzzling issue with the `np.roll` function when applied to masked arrays... My goal is to roll (shift) the elements of a masked array while preserving the masked values. However, it seems that the masked values are not being rolled as intended. Here's a simplified version of my code: ```python import numpy as np # Create a masked array with some values masked data = np.ma.array([1, 2, 3, 4, 5], mask=[0, 1, 0, 0, 1]) print("Original array:", data) # Attempting to roll the masked array rolled_data = np.roll(data, 2) print("Rolled array:", rolled_data) ``` When I run this code, the output is: ``` Original array: [1 -- 3 4 --] Rolled array: [4 -- 1 3 --] ``` The masked values (represented by `--`) seem to be shifted along with the unmasked values, which is not the intended behavior. I expected the rolled array to retain the original masked positions or at least preserve the data integrity related to the masked elements. I’ve checked the documentation, and it seems that `np.roll` should work directly with masked arrays. I also tried using `np.roll` on normal arrays and noticed that it behaves as expected without any issues. Additionally, I’ve considered using a combination of `np.ma.masked_array` and custom logic to handle the roll operation manually, but that feels overly complicated. Am I overlooking an option within `np.roll` that ensures masked values remain in their original positions, or is this a limitation of how NumPy handles masked arrays? Any insights or workarounds would be greatly appreciated! This is happening in both development and production on Debian. Thanks for taking the time to read this! I've been using Python for about a year now. Has anyone else encountered this? I'm working with Python in a Docker container on Ubuntu 20.04. Has anyone else encountered this?