dotnet/machinelearning · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values. (Pa

Error message

Specified argument was out of the range of valid values. (Parameter 'index')

What it means

The DataViewSchema.Column constructor requires a non-negative index; passing a negative index throws ArgumentOutOfRangeException naming 'index'. Column.Index must be a valid position within its schema.

Source

Thrown at src/Microsoft.ML.DataView/DataViewSchema.cs:110

            /// </summary>
            public bool IsHidden { get; }

            /// <summary>
            /// The type of the column.
            /// </summary>
            public DataViewType Type { get; }

            /// <summary>
            /// The annotations of the column.
            /// </summary>
            public Annotations Annotations { get; }

            internal Column(string name, int index, bool isHidden, DataViewType type, Annotations annotations)
            {
                if (string.IsNullOrEmpty(name))
                    throw new ArgumentNullException(nameof(name));
                if (index < 0)
                    throw new ArgumentOutOfRangeException(nameof(index));

                Name = name;
                Index = index;
                IsHidden = isHidden;
                Type = type ?? throw new ArgumentNullException(nameof(type));
                Annotations = annotations ?? Annotations.Empty;
            }

            public override string ToString()
            {
                var annotationsString = (Annotations == null || Annotations.Schema.Count == 0) ?
                    null : $" {{{string.Join(", ", Annotations.Schema.Select(x => x.Name))}}}";
                return $"{Name}: {Type}{annotationsString}";
            }
        }

        /// <summary>
        /// This class represents the schema of one column of a data view, without an attachment to a particular <see cref="DataViewSchema"/>.

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Verify the index is >= 0 before constructing; treat negative values as 'not found' and handle them instead.
  2. Check whether an earlier lookup API returned -1 for a missing name and add a branch for that case.
  3. Correct off-by-one/initialization bugs in loops that generate indices.
  4. Where available, build columns through Builder.Add, which assigns the index automatically.

Example fix

// before
var column = new Column(name, lookupIndex, false, type, null); // lookupIndex may be -1
// after
if (lookupIndex < 0) throw new KeyNotFoundException($"Column '{name}' not found");
var column = new Column(name, lookupIndex, false, type, null);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0) throw new KeyNotFoundException("Column lookup failed; index must be >= 0");

Type guard

bool IsValidColumnIndex(int index) => index >= 0;

Prevention

When it happens

Trigger: Constructing a Column with index < 0 — e.g. an index from a failed name lookup (some code returns -1 for 'not found') passed straight into the constructor, or a counter bug.

Common situations: Translating a -1 sentinel from a lookup miss directly into column construction; loops with wrong initialization; copying indices from a legacy structure that used -1 as 'unknown'.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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