question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DOC: numpy.ma.sort and copying behaviour

See original GitHub issue

I started out thinking I’ve found a docbug but I’m becoming less sure. The problem is that the documentation of numpy.ma.sort (devdocs version) says

Sort the array, in-place […] Returns: sorted_array

Whereas the behaviour seems to be the opposite:

>>> import numpy as np
>>> arr = np.arange(5)[::-1]
>>> np.ma.sort(arr)
array([0, 1, 2, 3, 4])
>>> arr
array([4, 3, 2, 1, 0])

The weird thing is that I’ve found https://github.com/numpy/numpy/issues/1966 which suggests that until 2010 the docs had mentioned a copy and the behaviour was mutating, and the docstring was subsequently fixed. However looking at the git blame even in commit 5cb370e923 from 2008 I can see

from numpy import array as narray
# [...]
def sort(...):
    a = narray(a, copy=True, subok=True)

This seems to me that even in 2008 it should have returned a copy, which makes the linked docbug issue very confusing. What am I missing?

Ultimately I think this is a docbug, because np.ma.sort should match np.sort which copies (and is documented as such), whereas the corresponding instance methods should sort in-place.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Apr 11, 2020

You could use .split("\n\n", 1)[1] to throw away the first paragraph, which would be less fragile.

Or we could just replace the whole docstring with equivalent to self.copy().sort() or similar, with a link to the other function.

0reactions
sebergcommented, Apr 11, 2020

I dunno, I think obviously broken is better than silently outdated. If you wish, add a test or assert mayb.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.ma.copy — NumPy v1.23 Manual
This function is the preferred method for creating an array copy. The function numpy.copy is similar, but it defaults to using order 'K',...
Read more >
NumPy manual contents
Constants · numpy.ma.MaskType · Creation · From existing data · numpy.ma. · Inspecting the array · numpy.ma.all · Manipulating a MaskedArray.
Read more >
jax.numpy package - JAX documentation - Read the Docs
msort (a). Return a copy of an array sorted along the first axis. ... which may have slightly different behavior than numpy.ndarray.astype() in...
Read more >
Source code for numpy.ma.core - Astropy
numpy.ma : a package to handle missing or invalid values. ... mask=nomask, copy=True, fill_value=None): """ Return input with invalid data masked and ...
Read more >
Supported NumPy features - Numba
For numeric dtypes, Numba follows Numpy's behavior. ... standard ufuncs that Numba is aware of, sorted in the same way as in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found