IterableDataset.add_column fails with arrow, pandas and polars formats
Describe the bug
IterableDataset.add_column() fails during iteration after with_format("arrow"), with_format("pandas"), or with_format("polars"). Adding the column before selecting the format works. This prevents a streaming data preparation pipeline from adding metadata columns after it has selected a table format.
On current main, add_column() delegates to map(add_column_fn, with_indices=True). The callback returns a dictionary, but table-formatted map() requires a table/DataFrame result and does not merge dictionary outputs with the input.
Related PR #8977 fixes rename_columns and explicitly leaves this add_column case out of scope. The feature-schema preservation proposals for #5752 address a separate problem; this report concerns the exception during iteration.
Steps to reproduce the bug
This uses in-memory data only, with no Hub access or downloaded dataset:
from datasets import IterableDataset
for fmt in ["arrow", "pandas", "polars"]:
ds = IterableDataset.from_dict({"a": [1, 2, 3], "b": ["x", "y", "z"]})
result = ds.with_format(fmt).add_column("c", [10, 20, 30])
try:
print(fmt, list(result))
except TypeError as exc:
print(fmt, exc)
Actual output:
arrow Provided `function` which is applied to arrow table returns a variable of type <class 'dict'>. Make sure provided `function` returns a arrow table to update the dataset.
pandas Provided `function` which is applied to pandas dataframe returns a variable of type <class 'dict'>. Make sure provided `function` returns a pandas dataframe to update the dataset.
polars Provided `function` which is applied to polars dataframe returns a variable of type <class 'dict'>. Make sure provided `function` returns a polars dataframe to update the dataset.
Expected behavior
Adding the column should preserve the requested output format and yield the same values as ds.add_column("c", [10, 20, 30]).with_format(fmt): the original a and b columns plus c = [10, 20, 30].
Environment info
- Windows, Python 3.12.10
- Datasets 5.2.0.dev0, main
a1269ca9dbb6e0858a9c375e8982329a3fcc84af - PyArrow 26.0.1, pandas 3.1.1, Polars 2.1.0
The same in-memory input succeeds without a table format. Investigation and this report were prepared with OpenAI Codex assistance. A focused fix and regression coverage are being prepared.
#self-assign