Skip to content

Reject a repeated column name in Dataset.remove_columns - #8529

Open
LeSingh1 wants to merge 2 commits into
huggingface:mainfrom
LeSingh1:ds-dup-columns
Open

Reject a repeated column name in Dataset.remove_columns#8529
LeSingh1 wants to merge 2 commits into
huggingface:mainfrom
LeSingh1:ds-dup-columns

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 30, 2026

Copy link
Copy Markdown

The bug

Dataset.remove_columns does not deduplicate its argument, so a repeated name deletes the same feature twice:

d = Dataset.from_dict({"a": [1, 2], "b": ["x", "y"]})
d.remove_columns(["a", "a"])
# KeyError: 'a'

The second del dataset._info.features[column_name] raises a bare KeyError, with nothing pointing at the repeat. A duplicate is easy to produce when the list is built programmatically, and the error does not say what was wrong.

The fix

Reject the repeat, in the style of the missing-column check that already sits directly above it.

Scope

I originally had this covering select_columns too, where a repeat produced a Dataset whose column_names was ['a', 'a'] while its features had one entry. @AbdullahRasheed45's #8368 already fixes that, on both Dataset and IterableDataset, so I have dropped that half — I only spotted #8368 after opening this. What is left is Dataset.remove_columns, which #8368 does not touch, so the two do not overlap.

I chose rejecting over silently deduplicating because quietly changing the result seemed worse than saying what is wrong, and it matches how #8368 handles the sibling method. Happy to make it a dedup instead if you would rather.

Verification

The new test fails on main and passes with the change, for both in_memory values. Comparing full tests/test_arrow_dataset.py runs with and without the change shows no test that fails only with it. One failure is present either way, test_dataset_to_iterable_dataset, which needs PyTorch and is not installed in my venv.

…lumns

Neither method deduplicated, and the two failure modes differed.

select_columns handed the repeated name to pyarrow, which built a table
with two identically named columns while Features kept one entry, so the
result was a Dataset whose column_names was ['a', 'a'] but whose features
had a single 'a'. No error at any point.

remove_columns deleted the same feature twice and surfaced the second
delete as a bare KeyError with no indication of what was wrong.

Reject the repeat in both, in the style of the missing-column check that
already sits above each.
PR huggingface#8368 already rejects a repeated name in select_columns, on both
Dataset and IterableDataset. Dropping that half here leaves only
Dataset.remove_columns, which huggingface#8368 does not touch.
@LeSingh1 LeSingh1 changed the title Reject a repeated column name in Dataset.select_columns and remove_columns Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant