def sample(self, n=None, frac=None, replace=False, weights=None,<EOL>random_state=None, axis=None):
if axis is None:<EOL><INDENT>axis = self._stat_axis_number<EOL><DEDENT>axis = self._get_axis_number(axis)<EOL>axis_length = self.shape[axis]<EOL>rs = com.random_state(random_state)<EOL>if weights is not None:<EOL><INDENT>if isinstance(weights, pd.Series):<EOL><INDENT>weights = weights.reindex(self.axes[axis])<EOL><DEDENT>if isinstance(weights, str):<EOL><INDENT>if isinstance(self, pd.DataFrame):<EOL><INDENT>if axis == <NUM_LIT:0>:<EOL><INDENT>try:<EOL><INDENT>weights = self[weights]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>weights = pd.Series(weights, dtype='<STR_LIT>')<EOL>if len(weights) != axis_length:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if (weights == np.inf).any() or (weights == -np.inf).any():<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if (weights < <NUM_LIT:0>).any():<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>weights = weights.fillna(<NUM_LIT:0>)<EOL>if weights.sum() != <NUM_LIT:1>:<EOL><INDENT>if weights.sum() != <NUM_LIT:0>:<EOL><INDENT>weights = weights / weights.sum()<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>weights = weights.values<EOL><DEDENT>if n is None and frac is None:<EOL><INDENT>n = <NUM_LIT:1><EOL><DEDENT>elif n is not None and frac is None and n % <NUM_LIT:1> != <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>elif n is None and frac is not None:<EOL><INDENT>n = int(round(frac * axis_length))<EOL><DEDENT>elif n is not None and frac is not None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if n < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>locs = rs.choice(axis_length, size=n, replace=replace, p=weights)<EOL>return self.take(locs, axis=axis, is_copy=False)<EOL>
Return a random sample of items from an axis of object.

You can use `random_state` for reproducibility.

Parameters
----------
n : int, optional
    Number of items from axis to return. Cannot be used with `frac`.
    Default = 1 if `frac` = None.
frac : float, optional
    Fraction of axis items to return. Cannot be used with `n`.
replace : bool, default False
    Sample with or without replacement.
weights : str or ndarray-like, optional
    Default 'None' results in equal probability weighting.
    If passed a Series, will align with target object on index. Index
    values in weights not found in sampled object will be ignored and
    index values in sampled object not in weights will be assigned
    weights of zero.
    If called on a DataFrame, will accept the name of a column
    when axis = 0.
    Unless weights are a Series, weights must be same length as axis
    being sampled.
    If weights do not sum to 1, they will be normalized to sum to 1.
    Missing values in the weights column will be treated as zero.
    Infinite values not allowed.
random_state : int or numpy.random.RandomState, optional
    Seed for the random number generator (if int), or numpy RandomState
    object.
axis : int or string, optional
    Axis to sample. Accepts axis number or name. Default is stat axis
    for given data type (0 for Series and DataFrames, 1 for Panels).

Returns
-------
Series or DataFrame
    A new object of same type as caller containing `n` items randomly
    sampled from the caller object.

See Also
--------
numpy.random.choice: Generates a random sample from a given 1-D numpy
    array.

Examples
--------
>>> df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
...                    'num_wings': [2, 0, 0, 0],
...                    'num_specimen_seen': [10, 2, 1, 8]},
...                   index=['falcon', 'dog', 'spider', 'fish'])
>>> df
        num_legs  num_wings  num_specimen_seen
falcon         2          2                 10
dog            4          0                  2
spider         8          0                  1
fish           0          0                  8

Extract 3 random elements from the ``Series`` ``df['num_legs']``:
Note that we use `random_state` to ensure the reproducibility of
the examples.

>>> df['num_legs'].sample(n=3, random_state=1)
fish      0
spider    8
falcon    2
Name: num_legs, dtype: int64

A random 50% sample of the ``DataFrame`` with replacement:

>>> df.sample(frac=0.5, replace=True, random_state=1)
      num_legs  num_wings  num_specimen_seen
dog          4          0                  2
fish         0          0                  8

Using a DataFrame column as weights. Rows with larger value in the
`num_specimen_seen` column are more likely to be sampled.

>>> df.sample(n=2, weights='num_specimen_seen', random_state=1)
        num_legs  num_wings  num_specimen_seen
falcon         2          2                 10
fish           0          0                  8