Skip to content

Commit cf187f8

Browse files
author
Francis T. O'Donovan
committed
DOC: Add examples for pandas.*.sample
The following doc files are generated from the sample() function from pandas/core/generic.py: pandas.DataFrame.sample.html pandas.Series.sample.html pandas.Panel.sample.html pandas.Panel4D.sample.html Examples have been added to the sample() function docstring for only the first two objects, since the latter two are rarely used.
1 parent a050a33 commit cf187f8

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

pandas/core/generic.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,10 @@ def bool(self):
748748

749749
def __abs__(self):
750750
return self.abs()
751-
751+
752752
def __round__(self,decimals=0):
753753
return self.round(decimals)
754-
754+
755755
#----------------------------------------------------------------------
756756
# Array Interface
757757

@@ -2189,6 +2189,48 @@ def sample(self, n=None, frac=None, replace=False, weights=None, random_state=No
21892189
Returns
21902190
-------
21912191
A new object of same type as caller.
2192+
2193+
Examples
2194+
--------
2195+
2196+
Generate an example ``Series`` and ``DataFrame``:
2197+
2198+
>>> s = pd.Series(np.random.randn(50))
2199+
>>> s.head()
2200+
0 -0.038497
2201+
1 1.820773
2202+
2 -0.972766
2203+
3 -1.598270
2204+
4 -1.095526
2205+
dtype: float64
2206+
>>> df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
2207+
>>> df.head()
2208+
A B C D
2209+
0 0.016443 -2.318952 -0.566372 -1.028078
2210+
1 -1.051921 0.438836 0.658280 -0.175797
2211+
2 -1.243569 -0.364626 -0.215065 0.057736
2212+
3 1.768216 0.404512 -0.385604 -1.457834
2213+
4 1.072446 -1.137172 0.314194 -0.046661
2214+
2215+
Next extract a random sample from both of these objects...
2216+
2217+
3 random elements from the ``Series``:
2218+
2219+
>>> s.sample(n=3)
2220+
27 -0.994689
2221+
55 -1.049016
2222+
67 -0.224565
2223+
dtype: float64
2224+
2225+
And a random 10% of the ``DataFrame`` with replacement:
2226+
2227+
>>> df.sample(frac=0.1, replace=True)
2228+
A B C D
2229+
35 1.981780 0.142106 1.817165 -0.290805
2230+
49 -1.336199 -0.448634 -0.789640 0.217116
2231+
40 0.823173 -0.078816 1.009536 1.015108
2232+
15 1.421154 -0.055301 -1.922594 -0.019696
2233+
6 -0.148339 0.832938 1.787600 -1.383767
21922234
"""
21932235

21942236
if axis is None:

0 commit comments

Comments
 (0)