-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
BUG: fix issue #8250 when np.array gets called on an invalid sequence #8570
BUG: fix issue #8250 when np.array gets called on an invalid sequence #8570
Conversation
Thanks, @evanlimanto. Could you please add a test to verify this behavior? |
2f062d1
to
41e789a
Compare
Added tests. I know I could've made a helper function (in duplicating the tests for shuffle), but I didn't want to disrupt the structure of the code. |
Looks good to me. I'll merge in a few days if there are no other objections |
Can this be merged soon? |
numpy/random/mtrand/mtrand.pyx
Outdated
@@ -4871,7 +4871,7 @@ cdef class RandomState: | |||
if isinstance(x, (int, long, np.integer)): | |||
arr = np.arange(x) | |||
else: | |||
arr = np.array(x) | |||
arr = x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hang on, this means that x
is now modified in place, rather than copied first. This isn't ok. Perhaps a copy.copy
here instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its also not good, that the tests did not pick it up.
41e789a
to
39d7d8c
Compare
Fixed, and added a test to check for mutation. |
numpy/random/mtrand/mtrand.pyx
Outdated
@@ -4871,7 +4871,8 @@ cdef class RandomState: | |||
if isinstance(x, (int, long, np.integer)): | |||
arr = np.arange(x) | |||
else: | |||
arr = np.array(x) | |||
import copy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this go at the top with the other imports?
numpy/random/tests/test_random.py
Outdated
desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) | ||
assert_array_equal(actual, desired) | ||
# check that array is not mutated. | ||
assert_array_equal(alist, conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be clearer immediately under the permutation
call
…rray gets called on an invalid sequence.
39d7d8c
to
7a73bad
Compare
Fixed, thanks! |
Looks good to me, as do the tests. The source of |
Thanks @evanlimanto |
This broke one of our tests downstream because this is now not returning an array (but a list). I don't think this is what you want? You are testing this input, but not that it actually returns an ndarray (just that it is equal). https://travis-ci.org/pandas-dev/pandas/jobs/210272568
|
Hmmm, maybe we should just revert and just change the documentation, its a safer bet. Is there much gain in allowing general sequences here? |
I think reversion would be the quick fix. I'm not sure there is a better fix. |
How about the following?
|
I think the problem is that an array should be returned by default, as previously happened. So at a minimum we still need
So perhaps use that as the fallback, although that may also lead to hard to interpret errors. |
BUG: fix issue numpy#8250 where np.random.permutation fail. This reverts commit 7a73bad. Closes numpy#8776.
Fixes the issue in #8250. We should let np.random.shuffle should handle the intricacies of the different types passed in.