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 'columnIndex')

What it means

DataViewSchema's indexer validates that columnIndex falls within the schema's column range before returning the Column. If the index is negative or >= the number of columns, an ArgumentOutOfRangeException naming 'columnIndex' is thrown. This guards against callers relying on stale or external indices that no longer match the schema.

Source

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

        {
            get
            {
                if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
                if (!_nameMap.TryGetValue(name, out int col))
                    throw new ArgumentOutOfRangeException(nameof(name), $"Column '{name}' not found");
                return _columns[col];
            }
        }

        /// <summary>
        /// Get the column by index.
        /// </summary>
        public Column this[int columnIndex]
        {
            get
            {
                if (!(0 <= columnIndex && columnIndex < _columns.Length))
                    throw new ArgumentOutOfRangeException(nameof(columnIndex));
                return _columns[columnIndex];
            }
        }

        /// <summary>
        /// Get the column by name, or <c>null</c> if the column is not present.
        /// </summary>
        public Column? GetColumnOrNull(string name)
        {
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
            if (_nameMap.TryGetValue(name, out int col))
                return _columns[col];
            return null;
        }

        public IEnumerator<Column> GetEnumerator() => ((IEnumerable<Column>)_columns).GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check the index against schema.Count before indexing: if (index >= 0 && index < schema.Count) { ... }.
  2. Instead of indices, look the column up by name with GetColumnOrNull(name) and use its Index property on the current schema.
  3. Recompute column indices from the schema you are actually using rather than caching them across transforms.
  4. Fix loop bounds to i < schema.Count (not <=).

Example fix

// before
var col = schema[columnIndex]; // throws when columnIndex out of range
// after
var col = columnIndex >= 0 && columnIndex < schema.Count ? schema[columnIndex] : null;
if (col == null) { /* handle missing column */ }
Defensive patterns

Strategy: validation

Validate before calling

bool isValidIndex = index >= 0 && index < schema.Count;

Type guard

bool HasColumn(DataViewSchema schema, int index) => schema != null && index >= 0 && index < schema.Count;

Prevention

When it happens

Trigger: Accessing schema[columnIndex] with a negative index, an index equal to or greater than schema.Count/_columns.Length, or an index captured from a different (older or other) schema whose column count shrank.

Common situations: Reusing column indices saved from a previous pipeline fit; iterating with an off-by-one loop (i <= Count); a transform dropped columns so an index from the input schema is invalid on the output schema.

Related errors


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