dotnet/machinelearning · error · ArgumentException

string.Format(Strings.MismatchedValueType, DataType)

Error message

string.Format(Strings.MismatchedValueType, DataType)

What it means

SetValue requires the boxed value to be exactly of type T (or null) since the column storage is T?; otherwise it throws ArgumentException naming the value parameter. DataFrameColumns are strongly typed, so a value of a different CLR type cannot be stored without conversion.

Source

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

        protected virtual PrimitiveDataFrameColumn<T> CreateNewColumn(string name, long length = 0)
        {
            return new PrimitiveDataFrameColumn<T>(name, length);
        }

        protected T? GetTypedValue(long rowIndex) => _columnContainer[rowIndex];

        protected override object GetValue(long rowIndex) => GetTypedValue(rowIndex);

        protected override void SetValue(long rowIndex, object value)
        {
            if (value == null || value.GetType() == typeof(T))
            {
                _columnContainer[rowIndex] = (T?)value;
            }
            else
            {
                throw new ArgumentException(string.Format(Strings.MismatchedValueType, DataType), nameof(value));
            }
        }

        public new T? this[long rowIndex]
        {
            get => GetTypedValue(rowIndex);
            set => _columnContainer[rowIndex] = value;
        }

        /// <inheritdoc/>
        public override double Median()
        {
            // Not the most efficient implementation. Using a selection algorithm here would be O(n) instead of O(nLogn)
            var notNullValuesCount = Length - NullCount;
            if (notNullValuesCount == 0)
                return 0;

            PrimitiveDataFrameColumn<long> sortIndices = GetSortIndices();

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Convert the value to the column's element type T before SetValue, e.g. (long)someInt
  2. Check column.DataType and dispatch conversion accordingly
  3. Use the strongly typed indexer on PrimitiveDataFrameColumn<T> instead of the base-class API

Example fix

// before
column.SetValue(row, 42); // column is PrimitiveDataFrameColumn<long>
// after
column.SetValue(row, (long)42);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value != null && value.GetType() != column.DataType) throw new ArgumentException($"Expected {column.DataType}, got {value.GetType()}");

Type guard

bool isCompatible(object v) => v == null || v.GetType() == typeof(T);

Try / catch

try { column.SetValue(row, value); } catch (ArgumentException) { /* convert value to T and retry */ }

Prevention

When it happens

Trigger: Calling SetValue(rowIndex, value) with a boxed value whose GetType() != typeof(T), e.g. storing int into a PrimitiveDataFrameColumn<long> or double into an int column.

Common situations: Populating columns from untyped data (JSON, CSV, database rows) where numerics arrive as different widths, or assigning a nullable/boxed value directly.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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