Random sample of items Parameters ---------- n : int, optional Number of items to return is not supported by dask. Use frac instead. frac : float, optional Approximate fraction of items to return. This sampling fraction is
(self, n=None, frac=None, replace=False, random_state=None)
| 2610 | return func(self, *args, **kwargs) |
| 2611 | |
| 2612 | def sample(self, n=None, frac=None, replace=False, random_state=None): |
| 2613 | """Random sample of items |
| 2614 | |
| 2615 | Parameters |
| 2616 | ---------- |
| 2617 | n : int, optional |
| 2618 | Number of items to return is not supported by dask. Use frac |
| 2619 | instead. |
| 2620 | frac : float, optional |
| 2621 | Approximate fraction of items to return. This sampling fraction is |
| 2622 | applied to all partitions equally. Note that this is an |
| 2623 | **approximate fraction**. You should not expect exactly ``len(df) * frac`` |
| 2624 | items to be returned, as the exact number of elements selected will |
| 2625 | depend on how your data is partitioned (but should be pretty close |
| 2626 | in practice). |
| 2627 | replace : boolean, optional |
| 2628 | Sample with or without replacement. Default = False. |
| 2629 | random_state : int or ``np.random.RandomState`` |
| 2630 | If an int, we create a new RandomState with this as the seed; |
| 2631 | Otherwise we draw from the passed RandomState. |
| 2632 | |
| 2633 | See Also |
| 2634 | -------- |
| 2635 | DataFrame.random_split |
| 2636 | pandas.DataFrame.sample |
| 2637 | """ |
| 2638 | if n is not None: |
| 2639 | msg = ( |
| 2640 | "sample does not support the number of sampled items " |
| 2641 | "parameter, 'n'. Please use the 'frac' parameter instead." |
| 2642 | ) |
| 2643 | if isinstance(n, Number) and 0 <= n <= 1: |
| 2644 | warnings.warn(msg) |
| 2645 | frac = n |
| 2646 | else: |
| 2647 | raise ValueError(msg) |
| 2648 | |
| 2649 | if frac is None: |
| 2650 | raise ValueError("frac must not be None") |
| 2651 | |
| 2652 | if random_state is None: |
| 2653 | random_state = np.random.RandomState() |
| 2654 | |
| 2655 | state_data = random_state_data(self.npartitions, random_state) |
| 2656 | return new_collection( |
| 2657 | expr.Sample(self, state_data=state_data, frac=frac, replace=replace) |
| 2658 | ) |
| 2659 | |
| 2660 | def _repr_data(self): |
| 2661 | raise NotImplementedError |