# Bucketing in Ray Dataset?

**URL:** https://discuss.ray.io/t/bucketing-in-ray-dataset/20631
**Category:** Uncategorized
**Created:** [November 15, 2024, 1:39pm UTC](https://discuss.ray.io/t/bucketing-in-ray-dataset/20631 "2024-11-15T13:39:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Eugene\_Zabrotsky](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/eugene_zabrotsky/32/7324_2.png) [@Eugene\_Zabrotsky](https://discuss.ray.io/u/Eugene_Zabrotsky)
#### Post date: [November 15, 2024, 1:39pm UTC](https://discuss.ray.io/t/bucketing-in-ray-dataset/20631/1 "2024-11-15T13:39:13Z")

</div>

We would like to implement bucketing in Ray Dataset.

We sort images into buckets based on their aspect ratio (how tall or wide), and then only emit batches such that all images in the batch belong to the same aspect ratio bucket. We decided that it’s impossible to implement with Ray Data’s transformations api, like map(), map\_batches(), filter(), etc.  
I would like you to help me confirm, that it’s impossible indeed.

So, the one way we see to solve this issue is to implement Custom Datasource which is basically analogous to ray.data.from\_torch().  
Is there any better approach? Did I make any incorrect assumptions previously?

---

<div class="post-metadata">

### Author: ![mowen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mowen/32/7309_2.png) [@mowen](https://discuss.ray.io/u/mowen)
#### Post date: [November 18, 2024, 6:54pm UTC](https://discuss.ray.io/t/bucketing-in-ray-dataset/20631/2 "2024-11-18T18:54:29Z")

</div>

Unfortunately this isn’t something that is natively supported very well. There is also a `groupby` method (docs [here](https://docs.ray.io/en/latest/data/api/doc/ray.data.Dataset.groupby.html)), which you can then use `map_groups` (docs [here](https://docs.ray.io/en/latest/data/api/doc/ray.data.grouped_data.GroupedData.map_groups.html#ray.data.grouped_data.GroupedData.map_groups)) to transform the groups. There are some limitations to this approach though. From the docs:

> While map\_groups() is very flexible, note that it comes with downsides:
> 
> - It may be slower than using more specific methods such as min(), max().
> - It requires that each group fits in memory on a single node.

The second point sounds like it might be an issue for you (depending on how big your buckets will be). If that is the case implementing a Custom Datasource might be the best path forward for now. Either way, please feel free to submit an issue to the Ray Github so we can track this feature request!
