bpd.Series.sample

Series.sample(n=None, replace=False, random_state=None)[source]

Return a random sample of elements from a Series.

You can use random_state for reproducibility.

Parameters:
  • n (None or int, optional) – Number of elements to return. None corresponds to 1.
  • replace ({False, True}, optional, keyword only.) – Sample with or without replacement.
  • random_state (int or numpy.random.RandomState, optional, keyword only) – Seed for the random number generator (if int), or numpy RandomState object.
Returns:

s_series – A new Series containing n items randomly sampled from the caller object.

Return type:

Series

Raises:

ValueError – If a sample larger than the length of the Series is taken without replacement.

Examples

>>> s = bpd.Series(data=[1, 2, 3, 4, 5])
>>> s.sample(3, random_state=0)
2    3
0    1
1    2
dtype: int64
>>> s.sample(7, replace=True, random_state=10)
1    2
4    5
0    1
1    2
3    4
4    5
1    2
dtype: int64