dotnet/machinelearning · error · ArgumentException
Training data has 0 rows
Error message
Training data has 0 rows
What it means
Thrown by UserInputValidationUtil.ValidateTrainData when the supplied IDataView is non-null but contains zero rows, as determined by DatasetDimensionsUtil.IsDataViewEmpty. AutoML cannot train or split an empty dataset, so the input is rejected with an ArgumentException naming trainData.
Solutions
- Check trainData.GetRow count / row cursor before executing; ensure the dataset has at least one row
- Fix the filter/query that eliminated all rows
- Verify the source file or enumerable actually contains data rows, not just a header
Example fix
// before
var data = mlContext.Data.LoadFromEnumerable(items.Where(x => x.Year == requestedYear));
var result = experiment.Execute(data, nameof(ModelInput.Label), "A");
// after
var filtered = items.Where(x => x.Year == requestedYear).ToList();
if (filtered.Count == 0) throw new InvalidOperationException("No training rows match the filter");
var data = mlContext.Data.LoadFromEnumerable(filtered);
var result = experiment.Execute(data, nameof(ModelInput.Label), "A"); Defensive patterns
Strategy: validation
Validate before calling
long rowCount = 0;
using (var cur = trainData.GetRowCursor(trainData.Schema))
while (cur.MoveNext()) { rowCount++; break; }
if (rowCount == 0) throw new InvalidOperationException("Training data has no rows"); Try / catch
try { var r = experiment.Execute(data, label, "A"); }
catch (ArgumentException ex) when (ex.Message.Contains("0 rows")) { /* surface upstream ETL failure */ } Prevention
- Assert row count > 0 after every data load/filter step
- Log source row counts in the ETL pipeline to catch empty outputs early
- Keep header-only and empty fixture files out of test data directories
When it happens
Trigger: Passing an IDataView built from an empty file, an empty DataFrame, a filtered view (e.g. after a FilterRowsByColumn predicate excluding everything), or an empty enumerable via LoadFromEnumerable.
Common situations: CSV with only a header row; date-range or category filters that exclude all rows at runtime; upstream ETL job produced no output; test fixture files empty after truncation.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Training data cannot be null
- column must be of data type
- Duplicate column name
- File ' ' does not exist
- If provided, must be the same as for Ranking Experiments
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/04ad29e6fc0d1e30.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.ML.AutoML/Utils/UserInputValidationUtil.cs:78
public static void ValidateSamplingKey(string samplingKeyColumnName, string groupIdColumnName, TaskKind task)
{
if (task == TaskKind.Ranking && samplingKeyColumnName != null && samplingKeyColumnName != groupIdColumnName)
{
throw new ArgumentException($"If provided, {nameof(samplingKeyColumnName)} must be the same as {nameof(groupIdColumnName)} for Ranking Experiments", samplingKeyColumnName);
}
}
private static void ValidateTrainData(IDataView trainData, ColumnInformation columnInformation)
{
if (trainData == null)
{
throw new ArgumentNullException(nameof(trainData), "Training data cannot be null");
}
if (DatasetDimensionsUtil.IsDataViewEmpty(trainData))
{
throw new ArgumentException("Training data has 0 rows", nameof(trainData));
}
foreach (var column in trainData.Schema)
{
if (column.Name == DefaultColumnNames.Features && column.Type.GetItemType() != NumberDataViewType.Single)
{
throw new ArgumentException($"{DefaultColumnNames.Features} column must be of data type {NumberDataViewType.Single}", nameof(trainData));
}
if ((column.Name != columnInformation.LabelColumnName &&
column.Name != columnInformation.UserIdColumnName &&
column.Name != columnInformation.ItemIdColumnName &&
column.Name != columnInformation.GroupIdColumnName)
&&
column.Type.GetItemType() != BooleanDataViewType.Instance &&
column.Type.GetItemType() != NumberDataViewType.Single &&
column.Type.GetItemType() != TextDataViewType.Instance)
{View on GitHub (pinned to 7b76e69cf9)