Skip to content

[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

Merged
merged 9 commits into from
Mar 17, 2020

Conversation

mikekutzma
Copy link
Contributor

@mikekutzma mikekutzma commented Mar 7, 2020

Alllow for array-like as well as BitGenerator inputs

Addresses: GH32503

Alllow for array-like as well as BitGenerator inputs

Addresses: GH32503
Copy link
Contributor

@TomAugspurger TomAugspurger left a 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.

if (
is_integer(state)
or is_array_like(state)
or isinstance(state, np.random.BitGenerator)
Copy link
Contributor

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.

@jreback jreback added Bug Numeric Operations Arithmetic, Comparison, and Logical operations labels Mar 11, 2020
@jreback jreback added this to the 1.1 milestone Mar 11, 2020
Copy link
Contributor

@jreback jreback left a 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.

@mikekutzma mikekutzma requested a review from jreback March 12, 2020 03:31
@@ -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
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

@mikekutzma mikekutzma requested a review from jreback March 15, 2020 02:20
Copy link
Contributor

@jreback jreback left a 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.

@@ -405,16 +407,27 @@ def random_state(state=None):
Returns
-------
np.random.RandomState

..versionchanged:: 1.1.0
Copy link
Contributor

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'

# 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))),
Copy link
Contributor

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)

@mikekutzma mikekutzma requested a review from jreback March 17, 2020 02:37
@jreback jreback merged commit 1193331 into pandas-dev:master Mar 17, 2020
SeeminSyed pushed a commit to CSCD01-team01/pandas that referenced this pull request Mar 22, 2020
jbrockmendel pushed a commit to jbrockmendel/pandas that referenced this pull request Mar 23, 2020
@alimcmaster1 alimcmaster1 mentioned this pull request May 22, 2020
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

core.common.random_state is too strict for state input
4 participants