Skip to content

Commit 87ac130

Browse files
author
Nick Eubank
committed
Updates to sinhrks comments,test for _random_state
1 parent 29f63ce commit 87ac130

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

doc/source/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,6 @@ Indexing, iteration
714714
DataFrame.where
715715
DataFrame.mask
716716
DataFrame.query
717-
DataFrame.sample
718717

719718
For more information on ``.at``, ``.iat``, ``.ix``, ``.loc``, and
720719
``.iloc``, see the :ref:`indexing documentation <indexing>`.
@@ -1074,6 +1073,7 @@ Reindexing / Selection / Label manipulation
10741073
Panel.reindex_axis
10751074
Panel.reindex_like
10761075
Panel.rename
1076+
Panel.sample
10771077
Panel.select
10781078
Panel.take
10791079
Panel.truncate

doc/source/whatsnew/v0.16.1.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ Highlights include:
1111

1212
- Support for a ``CategoricalIndex``, a category based index, see :ref:`here <whatsnew_0161.enhancements.categoricalindex>`
1313

14+
- New method ``sample`` for drawing random samples from Series, DataFrames and Panels. See :ref:`here <whatsnew_0161.enchancements.sample>`
15+
1416
.. contents:: What's new in v0.16.1
1517
:local:
1618
:backlinks: none
1719

18-
1920
.. _whatsnew_0161.enhancements:
2021

2122
Enhancements

pandas/core/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,17 +3320,18 @@ def _maybe_match_name(a, b):
33203320
return a_name
33213321
return None
33223322

3323-
def _random_state(state):
3323+
def _random_state(state=None):
33243324
"""
33253325
Helper function for processing random_state arguments.
33263326
33273327
Parameters
33283328
----------
3329-
state : int, np.random.RandomState, None
3329+
state : int, np.random.RandomState, None.
33303330
If receives an int, passes to np.random.RandomState() as seed.
33313331
If receives an np.random.RandomState object, just returns object.
33323332
If receives `None`, returns an np.random.RandomState object.
33333333
If receives anything else, raises an informative ValueError.
3334+
Default None.
33343335
33353336
Returns
33363337
-------

pandas/core/generic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,15 +1951,15 @@ def tail(self, n=5):
19511951

19521952
def sample(self, n=None, frac=None, replace=False, weights=None, random_state=None, axis = 0):
19531953
"""
1954-
Returns a random sample of rows from object.
1954+
Returns a random sample of items from an axis of object.
19551955
19561956
Parameters
19571957
----------
19581958
n : int, optional
1959-
Number of rows to return. Cannot be used with `frac`.
1959+
Number of items from axis to return. Cannot be used with `frac`.
19601960
Default = 1 if `frac` = None.
19611961
frac : float, optional
1962-
Fraction of rows to return. Cannot be used with `n`.
1962+
Fraction of axis items to return. Cannot be used with `n`.
19631963
replace : boolean, optional
19641964
Sample with or without replacement. Default = False.
19651965
weights : str or ndarray-like, optional

pandas/tests/test_common.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,26 @@ def test_is_recompilable():
524524
for f in fails:
525525
assert not com.is_re_compilable(f)
526526

527+
def test_random_state():
528+
import numpy.random as npr
529+
# Check with seed
530+
state = com._random_state(5)
531+
assert_equal(state.uniform(), npr.RandomState(5).uniform())
532+
533+
# Check with random state object
534+
state2 = npr.RandomState(10)
535+
assert_equal(com._random_state(state2).uniform(), npr.RandomState(10).uniform())
536+
537+
# check with no arg random state
538+
assert isinstance(com._random_state(), npr.RandomState)
539+
540+
# Error for floats or strings
541+
with tm.assertRaises(ValueError):
542+
com._random_state('test')
543+
544+
with tm.assertRaises(ValueError):
545+
com._random_state(5.5)
546+
527547

528548
class TestTake(tm.TestCase):
529549
# standard incompatible fill error

0 commit comments

Comments
 (0)