dotnet/machinelearning · error · System.FormatException

Empty file

Error message

Empty file

What it means

ReadCsvLinesIntoDataFrame counts the header/data rows it reads; if rowline == 0 nothing was read, meaning the input contains no lines, and it throws FormatException(Strings.EmptyFile). The library requires at least one line (header or data) to build columns.

Source

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

                                }
                            }
                            else
                            {
                                linesForGuessType.Add((rowline, fields));
                                numberOfColumns = Math.Max(numberOfColumns, fields.Length);
                            }
                        }
                    }
                    ++rowline;
                    if (rowline == guessRows || guessRows == 0)
                    {
                        break;
                    }
                }

                if (rowline == 0)
                {
                    throw new FormatException(Strings.EmptyFile);
                }

                columns = new List<DataFrameColumn>(numberOfColumns);
                // Guesses types or looks up dataTypes and adds columns.
                for (int i = 0; i < numberOfColumns; ++i)
                {
                    Type kind = dataTypes == null ? GuessKind(i, linesForGuessType, guessTypeFunction) : dataTypes[i];
                    columns.Add(CreateColumn(kind, columnNames, i));
                }
            }

            DataFrame ret = new DataFrame(columns);

            // Fill values.
            using (var textReader = wrappedReader.GetTextReader())
            {
                TextFieldParser parser = new TextFieldParser(textReader);
                parser.SetDelimiters(separator.ToString());

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check the file/input length before calling LoadCsv and handle empty inputs separately
  2. Fix the upstream producer so the CSV file is fully written before reading
  3. If empty files are legitimate, catch FormatException and return an empty DataFrame
  4. Verify the stream position (seek to 0) before passing it to LoadCsv

Example fix

// before
var df = DataFrame.LoadCsv(path);
// after
var lines = File.ReadAllLines(path);
if (lines.Length == 0) return new DataFrame();
var df = DataFrame.LoadCsv(new MemoryStream(Encoding.UTF8.GetBytes(string.Join("\n", lines))));
Defensive patterns

Strategy: try-catch

Validate before calling

var info = new FileInfo(path);
if (info.Length == 0) return new DataFrame(); // skip load

Try / catch

try { return DataFrame.LoadCsv(stream); } catch (FormatException ex) when (ex.Message == "Empty file") { return new DataFrame(); }

Prevention

When it happens

Trigger: Loading a zero-byte file or empty string via LoadCsv/LoadCsvFromString; a stream positioned at EOF; input containing only whitespace/BOM such that no rows are parsed.

Common situations: CSV export job produced an empty file; network transfer truncated the file; reading a file before it was written; opening the wrong path (an empty placeholder).

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


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