Describe the bug
Dataset.from_pandas memory usage scales with the chunk count of the backing Arrow array (around 1 MB per chunk).
Dataset.shuffle() followed by to_pandas() produces string columns with one chunk per row, so a 25k-row DataFrame holding ~32 MB of text allocates ~30 GB on the way back through from_pandas and the process gets killed silently. Encountered in Jupyter.
pyarrow.Table.from_pandas on the identical DataFrame completes in ~140 MB, so the amplification is likely in Dataset.
Steps to reproduce the bug
The following allocates ~30GB.
Open Activity Monitor (or equivalent), observe memory pressure when running the following code:
from datasets import Dataset
texts = ['lorem ipsum dolor sit amet consectetur ' * 34 for _ in range(25_000)] # ~1.3 KB each
ds = Dataset.from_dict({'text': texts, 'label': [0] * 25_000})
df = ds.shuffle(seed=42).to_pandas()
df['text'].array._pa_array.num_chunks # 25k, chunk per row
Dataset.from_pandas(df)
Measurements:
| scenario, 25k rows × ~1.3 KB |
chunks |
result |
shuffle() → to_pandas() → from_pandas() |
25,000 |
OOM |
| same, 6,000 rows |
6,000 |
OOM (>6.5 GB) |
no shuffle → from_pandas() |
25 |
OK, 139 MB |
shuffle() → flatten_indices() → from_pandas() |
25 |
OK, 184 MB |
shuffled df → pa.Table.from_pandas(df) |
25,000 |
OK, 141 MB |
Dataset.from_dict({'text': df['text'].tolist(), ...}) |
— |
OK, 468 MB |
Expected behavior
Memory proportional to data size, independent of chunk layout (ex. pyarrow.Table.from_pandas).
Possible workaround is to flatten_indices() before to_pandas(), or from_dict with .tolist() columns.
Environment info
datasets 5.0.0, pandas 3.0.4, pyarrow 24.0.0, Python 3.12.13
- macOS 14.6, Apple M3 Pro (arm64), 36 GB RAM
Describe the bug
Dataset.from_pandasmemory usage scales with the chunk count of the backing Arrow array (around 1 MB per chunk).Dataset.shuffle()followed byto_pandas()produces string columns with one chunk per row, so a 25k-row DataFrame holding ~32 MB of text allocates ~30 GB on the way back throughfrom_pandasand the process gets killed silently. Encountered in Jupyter.pyarrow.Table.from_pandason the identical DataFrame completes in ~140 MB, so the amplification is likely in Dataset.Steps to reproduce the bug
The following allocates ~30GB.
Open Activity Monitor (or equivalent), observe memory pressure when running the following code:
Measurements:
shuffle()→to_pandas()→from_pandas()from_pandas()shuffle()→flatten_indices()→from_pandas()pa.Table.from_pandas(df)Dataset.from_dict({'text': df['text'].tolist(), ...})Expected behavior
Memory proportional to data size, independent of chunk layout (ex.
pyarrow.Table.from_pandas).Possible workaround is to
flatten_indices()beforeto_pandas(), orfrom_dictwith.tolist()columns.Environment info
datasets5.0.0,pandas3.0.4,pyarrow24.0.0, Python 3.12.13