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.

np.copy(masked array) has unexpected behavior

See original GitHub issue

…where “unexpected” means I didn’t expect it. This may just be a documentation fix, or maybe I will be scolded for not realizing how np.ma works, but I don’t think this behavior is appropriate:

In [100]: x = np.ma.ones(5)

In [101]: x.mask = [False,False,True,False,False]

In [102]: np.copy(x)
Out[102]: array([ 1.,  1.,  1.,  1.,  1.])

In [103]: np.ma.copy(x)
Out[103]:
masked_array(data = [1.0 1.0 -- 1.0 1.0],
             mask = [False False  True False False],
       fill_value = 1e+20)

In [104]: np.__version__
Out[104]: '1.8.0.dev-074a40c'

I would at least expect a warning, but would prefer to see, in order of preference:

  • np.copy(x) returns a masked array (since x is a masked array)
  • np.copy(x) warns that it is unmasking the array, then returns the same array as above
  • np.copy(x) raises an exception
  • np.copy(x) returns [1,1,nan,1,1] (this is probably not ideal)

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jjhelmuscommented, Oct 19, 2015

I’ll send an email to the list on the topic tomorrow.

1reaction
jjhelmuscommented, Oct 19, 2015

Created PR #6509 which adds a subok parameter to the function with a default value of False. I’m guessing this will open up some discussion on the topic. Not sure if this typically takes place on the PR, the issue, or the mailing list.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected numpy.unique behavior - python - Stack Overflow
I am using numpy.unique to get values, indices and counts on a masked array that has been flattened with numpy.ravel and am getting...
Read more >
Unexpected behavior of trapz with MaskedArray #9735 - GitHub
I initially expected the numpy.trapz of a MaskedArray to behave as if trapz ... because only the first two values have contiguous elements, ......
Read more >
Source code for numpy.ma.core - Astropy
numpy.ma : a package to handle missing or invalid values. This package was initially written for numarray by Paul F. Dubois at Lawrence...
Read more >
ak.copy — Awkward Array documentation - Read the Docs
Returns a deep copy of the array (no memory shared with original). This is identical to np.copy and copy.deepcopy . It's only useful...
Read more >
1.4.1. The NumPy array object - Scipy Lecture Notes
What are NumPy and NumPy arrays? Creating arrays; Basic data types; Basic visualization; Indexing and slicing; Copies and views; Fancy indexing ...
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