{"record":{"id":"acf2c866a15bc549","repo":"dotnet/machinelearning","slug":"lesscolumnsthatexpected","errorCode":null,"errorMessage":"LessColumnsThatExpected","messagePattern":"LessColumnsThatExpected","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrame.IO.cs","lineNumber":63,"sourceCode":"                    else if (DateTime.TryParse(columnValue, out DateTime dateResult))\n                    {\n                        result = DetermineType(nbline == 0, typeof(DateTime), result);\n                    }\n                    else\n                    {\n                        result = DetermineType(nbline == 0, typeof(string), result);\n                    }\n\n                    nbline++;\n                }\n            }\n\n            return result;\n        }\n\n        private static Type GuessKind(int col, List<(long LineNumber, string[] Line)> read, Func<IEnumerable<string>, Type> guessTypeFunction)\n        {\n            IEnumerable<string> lines = read.Select(line => col < line.Line.Length ? line.Line[col] : throw new FormatException(string.Format(Strings.LessColumnsThatExpected, line.LineNumber + 1)));\n\n            return guessTypeFunction != null\n                ? guessTypeFunction.Invoke(lines)\n                : DefaultGuessTypeFunction(lines);\n        }\n\n        private static Type DetermineType(bool first, Type suggested, Type previous)\n        {\n            if (first)\n                return suggested;\n            else\n                return MaxKind(suggested, previous);\n        }\n\n        private static Type MaxKind(Type a, Type b)\n        {\n            if (a == typeof(string) || b == typeof(string))\n                return typeof(string);","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrame.IO.cs#L45-L81","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Open the file at the reported line number and fix the short/ragged row so all rows have the same field count","Verify the file's delimiter and pass the correct separator option to LoadCsv","Pre-validate that every line has the same number of fields as the header before loading","Regenerate or re-export the source file with consistent columns"],"exampleFix":"// before\nvar df = DataFrame.LoadCsv(\"data.csv\"); // row 42 has 3 fields, header has 5\n// after\nvar lines = File.ReadAllLines(\"data.csv\");\nvar expected = lines[0].Split(',').Length;\nif (lines.Any(l => l.Split(',').Length != expected))\n    throw new InvalidOperationException(\"CSV rows have inconsistent field counts\");\nvar df = DataFrame.LoadCsv(\"data.csv\");","handlingStrategy":"validation","validationCode":"var lines = File.ReadLines(path).ToList();\nint fieldCount = lines[0].Split(separator).Length;\nvar bad = lines.Select((l, i) => (l, i)).FirstOrDefault(x => x.l.Split(separator).Length != fieldCount);\nif (bad.l != null)\n    throw new FormatException($\"Line {bad.i + 1} has {bad.l.Split(separator).Length} fields, expected {fieldCount}\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var df = DataFrame.LoadCsv(path);\n}\ncatch (FormatException ex)\n{\n    // message includes the offending 1-based line number; inspect and repair that row\n    Console.WriteLine($\"Malformed CSV row: {ex.Message}\");\n}","preventionTips":["Pre-scan CSV files for ragged rows before loading","Confirm the actual delimiter matches the one passed to LoadCsv","Beware embedded separators/quotes; pre-clean or quote-escape source data","Check the reported line number (message is line index + 1) directly in the file"],"tags":["csv","format-exception","malformed-data","ragged-rows"],"backgroundTag":"schema-validation-failed","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}