dotnet/machinelearning · error · FormatException

LessColumnsThatExpected

Error message

LessColumnsThatExpected

What it means

During LoadFrom/FromCsv, DataFrame.IO.GuessKind throws FormatException(Strings.LessColumnsThatExpected, line number) when a data line has fewer fields than the column being typed. It collects line[col] for each line, and any line shorter than the target column index aborts the parse.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrame.IO.cs:63

                    else if (DateTime.TryParse(columnValue, out DateTime dateResult))
                    {
                        result = DetermineType(nbline == 0, typeof(DateTime), result);
                    }
                    else
                    {
                        result = DetermineType(nbline == 0, typeof(string), result);
                    }

                    nbline++;
                }
            }

            return result;
        }

        private static Type GuessKind(int col, List<(long LineNumber, string[] Line)> read, Func<IEnumerable<string>, Type> guessTypeFunction)
        {
            IEnumerable<string> lines = read.Select(line => col < line.Line.Length ? line.Line[col] : throw new FormatException(string.Format(Strings.LessColumnsThatExpected, line.LineNumber + 1)));

            return guessTypeFunction != null
                ? guessTypeFunction.Invoke(lines)
                : DefaultGuessTypeFunction(lines);
        }

        private static Type DetermineType(bool first, Type suggested, Type previous)
        {
            if (first)
                return suggested;
            else
                return MaxKind(suggested, previous);
        }

        private static Type MaxKind(Type a, Type b)
        {
            if (a == typeof(string) || b == typeof(string))
                return typeof(string);

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Open the file at the reported line number and fix the short/ragged row so all rows have the same field count
  2. Verify the file's delimiter and pass the correct separator option to LoadCsv
  3. Pre-validate that every line has the same number of fields as the header before loading
  4. Regenerate or re-export the source file with consistent columns

Example fix

// before
var df = DataFrame.LoadCsv("data.csv"); // row 42 has 3 fields, header has 5
// after
var lines = File.ReadAllLines("data.csv");
var expected = lines[0].Split(',').Length;
if (lines.Any(l => l.Split(',').Length != expected))
    throw new InvalidOperationException("CSV rows have inconsistent field counts");
var df = DataFrame.LoadCsv("data.csv");
Defensive patterns

Strategy: validation

Validate before calling

var lines = File.ReadLines(path).ToList();
int fieldCount = lines[0].Split(separator).Length;
var bad = lines.Select((l, i) => (l, i)).FirstOrDefault(x => x.l.Split(separator).Length != fieldCount);
if (bad.l != null)
    throw new FormatException($"Line {bad.i + 1} has {bad.l.Split(separator).Length} fields, expected {fieldCount}");

Try / catch

try
{
    var df = DataFrame.LoadCsv(path);
}
catch (FormatException ex)
{
    // message includes the offending 1-based line number; inspect and repair that row
    Console.WriteLine($"Malformed CSV row: {ex.Message}");
}

Prevention

When it happens

Trigger: Loading a CSV/flat file via DataFrame.LoadCsv where some rows have fewer comma-separated fields than the header/column count — e.g. ragged rows, malformed quoting, or truncated lines.

Common situations: Hand-edited or partially written CSV files; embedded commas/newlines breaking naive parsing; files exported by tools that omit trailing empty fields; wrong separator assumption (e.g. semicolon-delimited data read as comma).

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/acf2c866a15bc549. Report an issue: GitHub.