HFTrainer fails with pandas DataFrames (pandas >= 2.1 DataFrame.map is mistaken for a Hugging Face dataset)
Author: huiq777Created Sep 24, 2026Updated Sep 24, 2026
HFTrainer fails when given a pandas DataFrame, although Data.prepare and Data.labels have DataFrame branches for this case.
import pandas as pd
from txtai.pipeline import HFTrainer
data = [{"text": "Dogs", "label": 0}, {"text": "dog", "label": 0}, {"text": "Cats", "label": 1}, {"text": "cat", "label": 1}]
HFTrainer()("google/bert_uncased_L-2_H-128_A-2", pd.DataFrame(data))AttributeError: 'DataFrame' object has no attribute 'unique'. Did you mean: 'nunique'?There are three separate problems in txtai/data/base.py:
- pandas DataFrames are treated as Hugging Face datasets.
Data.labelsandData.preparedetect a dataset withhasattr(data, "map").pandas.DataFrame.mapwas added in pandas 2.1 (it replacesapplymap), so any DataFrame now takes the dataset branch and callsdata.unique(...)/data.map(..., remove_columns=data.column_names). This affects every pandas DataFrame on pandas >= 2.1. I tested with pandas 3.0.6. - A validation DataFrame raises.
Data.__call__uses... if validation else None. Truth-testing a DataFrame raises: pandas raisesValueError: The truth value of a DataFrame is ambiguousand polars raisesTypeError: the truth value of a DataFrame is ambiguous. A polars train DataFrame alone trains fine. Addingvalidation=a polars DataFrame fails with thatTypeError. Data.labelsreads the first label by index label, not by position.data[column][0]on a pandas Series looks up the index label0. After a shuffle or split (for exampledf.sample(frac=1)ortrain_test_split), label0is usually not first and can be missing, which raisesKeyError: 0.
testDataFrame uses a mock DataFrame that has no map attribute and a list-backed column, so none of these show up in the tests.
Proposed fix:
- Detect Hugging Face datasets by
column_names, which the dataset branch already uses and DataFrames don't have.datasets.Datasethas nocolumnsattribute, so the two branches no longer overlap. - Use
validation is not None. - Read the first value with
next(iter(data[column])).
I'll also add a test that uses a real pandas DataFrame, with a shuffled index and a validation set. pandas is already installed with the all extras. I have the fix ready and will open a PR referencing this issue.
Source: neuml/txtai