dotnet/machinelearning · error · ArgumentException

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

Error message

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

What it means

FillNullsImplementation accepts only object values that are string; anything else throws ArgumentException(MismatchedValueType, typeof(string)). It is the object-dispatch layer for the strongly typed FillNulls(string, bool).

Source

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

        {
            if (value == null)
                throw new ArgumentNullException(nameof(value));
            StringDataFrameColumn column = inPlace ? this : Clone();

            for (long i = 0; i < column.Length; i++)
            {
                if (column[i] == null)
                    column[i] = value;
            }
            return column;
        }

        protected override DataFrameColumn FillNullsImplementation(object value, bool inPlace)
        {
            if (value is string valueString)
                return FillNulls(valueString, inPlace);
            else
                throw new ArgumentException(String.Format(Strings.MismatchedValueType, typeof(string)), nameof(value));
        }

        /// <inheritdoc/>
        public new StringDataFrameColumn DropNulls()
        {
            return (StringDataFrameColumn)DropNullsImplementation();
        }

        protected override DataFrameColumn DropNullsImplementation()
        {
            var ret = new StringDataFrameColumn(Name, Length - NullCount);

            long j = 0;
            for (long i = 0; i < Length; i++)
            {
                var value = this[i];

                if (value != null)

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Pass a string fill value, e.g. column.FillNulls("N/A", inPlace: true)
  2. Convert the fill value with value.ToString() before calling
  3. Branch on column.DataType and choose the right fill value per column type
  4. Skip string columns in generic fill loops or handle them with string literals

Example fix

// before
column.FillNullsImplementation(0, true);
// after
column.FillNullsImplementation("0", true);
Defensive patterns

Strategy: type-guard

Validate before calling

if (fillValue is not string) throw new ArgumentException("Fill value for StringDataFrameColumn must be a string");

Type guard

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

Try / catch

try { column.FillNulls(fillValue, true); } catch (ArgumentException) { /* convert fill value and retry */ }

Prevention

When it happens

Trigger: Calling the polymorphic FillNullsImplementation or object-based FillNulls with an int, double, DateTime, etc. as the fill value.

Common situations: Passing default fill constants (0, empty DateTime) intended for numeric/date columns to a string column; generic fill code that boxes values.

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