dotnet/machinelearning · error · System.ArgumentException

Expected either {0} or {1} to be provided

Error message

Expected either {0} or {1} to be provided

What it means

ReadCsvLinesIntoDataFrame requires either explicit column dataTypes or a positive guessRows count to determine column schemas. When both dataTypes is null and guessRows <= 0, it throws ArgumentException with Strings.ExpectedEitherGuessRowsOrDataTypes formatted as 'Expected either guessRows or dataTypes to be provided'. This is an argument-validation guard at CSV schema resolution.

Source

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

        {
            return CreateColumn(kind, GetColumnName(columnNames, columnIndex));
        }

        private static DataFrame ReadCsvLinesIntoDataFrame(WrappedStreamReaderOrStringReader wrappedReader,
                                char separator = ',', bool header = true,
                                string[] columnNames = null, Type[] dataTypes = null,
                                long numberOfRowsToRead = -1, int guessRows = 10, bool addIndexColumn = false,
                                bool renameDuplicatedColumns = false,
                                CultureInfo cultureInfo = null, Func<IEnumerable<string>, Type> guessTypeFunction = null)
        {
            if (cultureInfo == null)
            {
                cultureInfo = CultureInfo.CurrentCulture;
            }

            if (dataTypes == null && guessRows <= 0)
            {
                throw new ArgumentException(string.Format(Strings.ExpectedEitherGuessRowsOrDataTypes, nameof(guessRows), nameof(dataTypes)));
            }

            List<DataFrameColumn> columns;
            string[] fields;
            using (var textReader = wrappedReader.GetTextReader())
            {
                TextFieldParser parser = new TextFieldParser(textReader);
                parser.SetDelimiters(separator.ToString());

                var linesForGuessType = new List<(long LineNumber, string[] Line)>();
                long rowline = 0;
                int numberOfColumns = dataTypes?.Length ?? 0;

                if (header == true && numberOfRowsToRead != -1)
                {
                    numberOfRowsToRead++;
                }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Pass a positive guessRows value (default is 10) so types can be inferred
  2. Or provide an explicit dataTypes array matching the number of columns
  3. Check the call site to ensure dataTypes wasn't unintentionally null
  4. Use LoadCsv's defaults rather than overriding guessRows with 0

Example fix

// before
var df = DataFrame.LoadCsv(stream, guessRows: 0);
// after
var df = DataFrame.LoadCsv(stream, guessRows: 10); // or supply dataTypes: new[]{typeof(long), typeof(string)}
Defensive patterns

Strategy: validation

Validate before calling

if (dataTypes == null && guessRows <= 0) throw new ArgumentException("Provide dataTypes or a positive guessRows");

Try / catch

try { return DataFrame.LoadCsv(stream, guessRows: guessRows); } catch (ArgumentException ex) when (ex.Message.Contains("guessRows")) { return DataFrame.LoadCsv(stream, guessRows: 10); }

Prevention

When it happens

Trigger: Calling ReadCsvLinesIntoDataFrame (directly or via LoadCsvFromString/LoadCsv) with dataTypes == null and guessRows <= 0, e.g. LoadCsv(stream, guessRows: 0) without dataTypes.

Common situations: Passing guessRows: 0 or -1 thinking it disables guessing; supplying dataTypes: null by accident after refactoring; copying a LoadCsv call and stripping arguments.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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