8000 BUG: Fix masked array raveling when `order="A"` or `order="K"` by seberg · Pull Request #23626 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix masked array raveling when order="A" or order="K" #23626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4631,6 +4631,7 @@ def ravel(self, order='C'):
otherwise. 'K' means to read the elements in the order they occur
in memory, except for reversing the data when strides are negative.
By default, 'C' index order is used.
(Masked arrays currently use 'A' on the data when 'K' is passed.)

Returns
-------
Expand All @@ -4657,6 +4658,13 @@ def ravel(self, order='C'):
fill_value=999999)

"""
# The order of _data and _mask could be different (it shouldn't be
# normally). Passing order `K` or `A` would be incorrect.
# So we ignore the mask memory order.
# TODO: We don't actually support K, so use A instead. We could
# try to guess this correct by sorting strides or deprecate.
if order in "kKaA":
order = "C" if self._data.flags.fnc else "F"
r = ndarray.ravel(self._data, order=order).view(type(self))
r._update_from(self)
if self._mask is not nomask:
Expand Down
16 changes: 16 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,22 @@ def test_ravel(self):
assert_equal(a.ravel(order='C'), [1, 2, 3, 4])
assert_equal(a.ravel(order='F'), [1, 3, 2, 4])

@pytest.mark.parametrize("order", "AKCF")
@pytest.mark.parametrize("data_order", "CF")
def test_ravel_order(self, order, data_order):
# Ravelling must ravel mask and data in the same order always to avoid
# misaligning the two in the ravel result.
arr = np.ones((5, 10), order=data_order)
arr[0, :] = 0
mask = np.ones((10, 5), dtype=bool, order=data_order).T
mask[0, :] = False
x = array(arr, mask=mask)
assert x._data.flags.fnc != x._mask.flags.fnc
assert (x.filled(0) == 0).all()
raveled = x.ravel(order)
assert (raveled.filled(0) == 0).all()


def test_reshape(self):
# Tests reshape
x = arange(4)
Expand Down
0