dotnet/machinelearning · error · ArgumentException

string.Format(Strings.MismatchedValueType, typeof(string))

Error message

string.Format(Strings.MismatchedValueType, typeof(string))

What it means

StringDataFrameColumn.SetValue (and the object indexer) requires the assigned value to be a string. Passing any other object type throws ArgumentException formatted with Strings.MismatchedValueType naming typeof(string).

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumns/StringDataFrameColumn.cs:153

        protected override void SetValue(long rowIndex, object value)
        {
            if (value == null || value is string)
            {
                int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);
                int bufferOffset = (int)(rowIndex % MaxCapacity);
                var oldValue = this[rowIndex];
                _stringBuffers[bufferIndex][bufferOffset] = (string)value;
                if (oldValue != (string)value)
                {
                    if (value == null)
                        _nullCount++;
                    if (oldValue == null && _nullCount > 0)
                        _nullCount--;
                }
            }
            else
            {
                throw new ArgumentException(string.Format(Strings.MismatchedValueType, typeof(string)), nameof(value));
            }
        }

        public new string this[long rowIndex]
        {
            get => (string)GetValue(rowIndex);
            set => SetValue(rowIndex, value);
        }

        public new List<string> this[long startIndex, int length]
        {
            get
            {
                var ret = new List<string>();
                int bufferIndex = GetBufferIndexContainingRowIndex(startIndex);
                int bufferOffset = (int)(startIndex % MaxCapacity);
                while (ret.Count < length && bufferIndex < _stringBuffers.Count)
                {

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Convert the value to a string before assignment (e.g. .ToString() or Convert.ToString)
  2. Use the strongly typed string indexer column[rowIndex] = someString
  3. Validate value is string before calling SetValue
  4. Check the source column's DataType before copying values across columns

Example fix

// before
column[rowIndex] = 42;
// after
column[rowIndex] = 42.ToString();
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not string s) throw new ArgumentException($"Expected string, got {value?.GetType().Name}");

Type guard

bool IsStringValue(object v) => v is string;

Try / catch

try { column[rowIndex] = value; } catch (ArgumentException e) { /* value type mismatch; convert and retry */ }

Prevention

When it happens

Trigger: Assigning a non-string (e.g. int, DateTime, null-typed object) via the non-generic this[long] object indexer or SetValue(rowIndex, value).

Common situations: Copying values from a loosely-typed row into a string column; boxing numeric values into the object indexer; dynamic/JSON data with unexpected element types.

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/bc89230a2e9dc21e. Report an issue: GitHub.