-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Should np.ma.masked be hashable? #4660
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
Comments
# Python 3.4, NumPy 1.9.3
import numpy as np
a = np.ndarray(1)
b = np.ma.MaskedArray()
hash(a)
# TypeError: unhashable type: 'numpy.ndarray'
hash(b)
# TypeError: unhashable type: 'MaskedArray'
hash(np.ma.masked)
# TypeError: unhashable type: 'MaskedConstant' How can you change the contents of print(np.ma.masked._data)
# 0.0
np.ma.masked._data[()] = 1
print(np.ma.masked._data)
# 1.0 |
The example |
@ahaldane Correct, From a git bisect is looks like PR #5326 fixed the old broken behavior where This behavior is not covered by any unit tests, it may be prudent to add tests along the lines of: import collections
def test_ma_collections_hashable():
x = np.ma.MaskedArray([])
assert not isinstance(x, collections.Hashable)
y = np.ma.masked
assert not isinstance(y, collections.Hashable) Such tests might currently fail in Windows due to issue #5647. |
Coming back to this because of astropy/astropy#6153 (comment), I still don't understand why Indeed if you try to change it via public methods an exception is raised. (I'm not concerned that there is a back-door private attribute, if somebody messes with np.ma.masked in that way then they get what they deserve).
|
This no longer works
I can see one argument: it represents an unknown value, and an unknown value has an unknown hash. |
but |
Somewhat on-topic: Would it make sense that when an array's |
That's an interesting idea, but unfortunately doesn't work too well - I can create a read-only view of a writeable array, and the hash will vary with the source array |
It seems that in python 2.7 I can do
a = {np.ma.masked: 'AAA'}
while in python3 this code throwsTypeError: unhashable type: 'MaskedConstant'
(numpy 1.8. in both cases).The text was updated successfully, but these errors were encountered: