Skip to content

Dataset.from_pandas allocates ~1 MB per Arrow chunk, Out of Memory errors #8327

Description

@nenadom

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions