#1265·txtai

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.

python
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:

  1. pandas DataFrames are treated as Hugging Face datasets. Data.labels and Data.prepare detect a dataset with hasattr(data, "map"). pandas.DataFrame.map was added in pandas 2.1 (it replaces applymap), so any DataFrame now takes the dataset branch and calls data.unique(...) / data.map(..., remove_columns=data.column_names). This affects every pandas DataFrame on pandas >= 2.1. I tested with pandas 3.0.6.
  2. A validation DataFrame raises. Data.__call__ uses ... if validation else None. Truth-testing a DataFrame raises: pandas raises ValueError: The truth value of a DataFrame is ambiguous and polars raises TypeError: the truth value of a DataFrame is ambiguous. A polars train DataFrame alone trains fine. Adding validation= a polars DataFrame fails with that TypeError.
  3. Data.labels reads the first label by index label, not by position. data[column][0] on a pandas Series looks up the index label 0. After a shuffle or split (for example df.sample(frac=1) or train_test_split), label 0 is usually not first and can be missing, which raises KeyError: 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.Dataset has no columns attribute, 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.