-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[BUG] Loosen random_state input restriction #32510
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
Conversation
Alllow for array-like as well as BitGenerator inputs Addresses: GH32503
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 you add a release note to v1.1.0.rst?
Need to update the docstring at https://github.com/pandas-dev/pandas/pull/32510/files#diff-21cb6655a9828d526610249e3d6cb16eR398, and make sure to include a .. versionmodified:: 1.1.0
noting that support for arrays and BitGenerators was added in 1.1.
pandas/core/common.py
Outdated
if ( | ||
is_integer(state) | ||
or is_array_like(state) | ||
or isinstance(state, np.random.BitGenerator) |
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.
BitGenerator is somewhat new (NumPy>=1.17 IIRC). You'll need to put this in a conditional check. pandas.compat
has some helpers.
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 you also update the doc-string for .sample in pandas.core.generic
and also update a test to use a BItGenerator (grep for where these tests are)
I would actually change the whatsnew to mention .sample which is much more user facing.
pandas/tests/generic/test_generic.py
Outdated
@@ -654,6 +657,24 @@ def test_sample(sel): | |||
with pytest.raises(ValueError): | |||
df.sample(1, weights=s4) | |||
|
|||
# Check that random_state arguments are processed correctly |
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 you make this a separate test & parameterize
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.
Done 👍
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.
looks good, some doc-string changes, ping on green.
pandas/core/common.py
Outdated
@@ -405,16 +407,27 @@ def random_state(state=None): | |||
Returns | |||
------- | |||
np.random.RandomState | |||
|
|||
..versionchanged:: 1.1.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 needs to be in the Paramters section, just below the 'If receives anything'
pandas/tests/generic/test_generic.py
Outdated
# GH32503 | ||
df = pd.DataFrame({"col1": range(10, 20), "col2": range(20, 30)}) | ||
tm.assert_frame_equal( | ||
df.sample(n=3, random_state=com.random_state(eval(func_str)(arg))), |
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 you do
result =
expected =
tm.assert_frame_equal(result, expected)
Alllow for array-like as well as BitGenerator inputs
Addresses: GH32503
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff