dotnet/machinelearning · error · IndexOutOfRangeException

row

Error message

row

What it means

PrimitiveDataFrameColumn<T>.ApplyElementwise (the indexer-style setter) only allows writing at positions already occupied or exactly at the end. If the given row index is greater than the current Length, the column cannot sparse-fill the gap, so it throws IndexOutOfRangeException naming the 'row' parameter.

Source

Thrown at src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs:914

        protected internal override void AddValueUsingCursor(DataViewRowCursor cursor, Delegate getter)
        {
            long row = cursor.Position;
            T value = default;
            Debug.Assert(getter != null, "Excepted getter to be valid");
            (getter as ValueGetter<T>)(ref value);

            if (Length > row)
            {
                this[row] = value;
            }
            else if (Length == row)
            {
                Append(value);
            }
            else
            {
                throw new IndexOutOfRangeException(nameof(row));
            }
        }

        protected internal override Delegate GetValueGetterUsingCursor(DataViewRowCursor cursor, DataViewSchema.Column schemaColumn)
        {
            return cursor.GetGetter<T>(schemaColumn);
        }

        public override Dictionary<long, ICollection<long>> GetGroupedOccurrences(DataFrameColumn other, out HashSet<long> otherColumnNullIndices)
        {
            return GetGroupedOccurrences<T>(other, out otherColumnNullIndices);
        }

        public override PrimitiveDataFrameColumn<bool> ElementwiseIsNull()
        {
            var ret = new BooleanDataFrameColumn(Name, Length);

            for (long i = 0; i < Length; i++)

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure the write index is < column.Length, or exactly == Length to append
  2. Grow the column first by calling Append(value) for each missing row before random writes
  3. Re-derive the row index from the DataFrame/cursor being iterated rather than a stale counter
  4. Use DataFrameColumn length checks (index >= Length) in the callback and skip or extend accordingly

Example fix

// before
col[row] = value; // row > col.Length -> IndexOutOfRangeException
// after
if (row == col.Length) col.Append(value);
else if (row < col.Length) col[row] = value;
Defensive patterns

Strategy: validation

Validate before calling

if (row < 0 || row > column.Length)
    throw new ArgumentOutOfRangeException(nameof(row), $"row {row} is outside [0, {column.Length}]; only == Length appends are allowed");

Type guard

bool IsValidRow(PrimitiveDataFrameColumn<T> col, long row) => row >= 0 && row <= col.Length;

Try / catch

try
{
    column[row] = value;
}
catch (IndexOutOfRangeException ex)
{
    // log ex.Message ('row') and clamp/append instead
}

Prevention

When it happens

Trigger: Calling an element-wise apply/set API (e.g. ApplyElementwise or the column setter used by DataFrame row assignment) with a row index > column.Length, i.e. attempting to append beyond the end instead of exactly at Length.

Common situations: Building a column by assigning at computed indices after rows were dropped or filtered (Length shrank); misaligned row counters between two columns; off-by-one when the loop counter starts at Length instead of ending at it.

Related errors


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