# How do I sample from a Ray Datasets

**URL:** <https://discuss.ray.io/t/how-do-i-sample-from-a-ray-datasets/5308>\
**Category:** Ray Data\
**Created:** [March 4, 2022, 10:12pm UTC](https://discuss.ray.io/t/how-do-i-sample-from-a-ray-datasets/5308 "2022-03-04T22:12:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon-mo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/simon-mo/32/9_2.png) [@simon-mo](https://discuss.ray.io/u/simon-mo)\
**Post date:** [March 4, 2022, 10:12pm UTC](https://discuss.ray.io/t/how-do-i-sample-from-a-ray-datasets/5308/1 "2022-03-04T22:12:22Z")

</div>

In pandas there is a `sample` API, how can I do that in Ray Datasets?

My traceback:

```auto
...
  File "starter.py", line 35, in train_xgboost
    X_train = data.sample(frac=1 - test_fraction)
AttributeError: 'Dataset' object has no attribute 'sample'

```

Thank you!

---

<div class="post-metadata">

**Author:** ![ericl](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ericl/32/133_2.png) [@ericl](https://discuss.ray.io/u/ericl)\
**Post date:** [May 3, 2022, 11:34pm UTC](https://discuss.ray.io/t/how-do-i-sample-from-a-ray-datasets/5308/2 "2022-05-03T23:34:17Z")

</div>

I filed this GitHub issue to add random\_sample(): [[data] Add dataset.random\_sample() API · Issue #24449 · ray-project/ray · GitHub](https://github.com/ray-project/ray/issues/24449)

For now, you can `ds.map_batches(lambda batch: batch.sample(...)).take()` to implement random sampling “manually”.
