dotnet/machinelearning · error · ArgumentException

Expected column to hold values of type {0}

Error message

Expected column to hold values of type {0}

What it means

For a decimal column T=decimal, binary operations with a column of a different non-bool type U are only supported out-of-place: the library clones itself as decimal and operates on a decimal-converted right column. If inPlace is true and typeof(U) != typeof(T), it throws ArgumentException (Strings.MismatchedColumnValueType, 'Expected column to hold values of type {0}') because the result type would differ from the in-place target.

Source

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

                    throw new NotSupportedException();
                case Type decimalType when decimalType == typeof(decimal):
                    if (typeof(U) == typeof(bool))
                    {
                        throw new NotSupportedException();
                    }
                    if (typeof(U) == typeof(T))
                    {
                        // No conversions
                        PrimitiveDataFrameColumn<U> primitiveColumn = this as PrimitiveDataFrameColumn<U>;
                        PrimitiveDataFrameColumn<U> newColumn = inPlace ? primitiveColumn : primitiveColumn.Clone();
                        newColumn._columnContainer.HandleOperation(operation, column._columnContainer);
                        return newColumn;
                    }
                    else
                    {
                        if (inPlace)
                        {
                            throw new ArgumentException(string.Format(Strings.MismatchedColumnValueType, typeof(T)), nameof(column));
                        }
                        PrimitiveDataFrameColumn<decimal> decimalColumn = CloneAsDecimalColumn();
                        decimalColumn._columnContainer.HandleOperation(operation, column.CloneAsDecimalColumn()._columnContainer);
                        return decimalColumn;
                    }
                case Type DateTimeType when DateTimeType == typeof(DateTime):
                    throw new NotSupportedException();
                case Type byteType when byteType == typeof(byte):
                case Type charType when charType == typeof(char):
                case Type doubleType when doubleType == typeof(double):
                case Type floatType when floatType == typeof(float):
                case Type intType when intType == typeof(int):
                case Type longType when longType == typeof(long):
                case Type sbyteType when sbyteType == typeof(sbyte):
                case Type shortType when shortType == typeof(short):
                case Type uintType when uintType == typeof(uint):
                case Type ulongType when ulongType == typeof(ulong):
                case Type ushortType when ushortType == typeof(ushort):

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Use the non-in-place operation (inPlace: false) so the result is a new decimal column
  2. Convert the right-hand column to decimal first (CloneAsDecimalColumn or Cast) so typeof(U) == typeof(T)
  3. Change the right column's construction to produce PrimitiveDataFrameColumn<decimal>

Example fix

// before
var result = decimalColumn.Add(intColumn, inPlace: true); // ArgumentException
// after
var result = decimalColumn.Add(intColumn, inPlace: false); // returns decimal column
// or
var result = decimalColumn.Add(intColumn.CloneAsDecimalColumn(), inPlace: true);
Defensive patterns

Strategy: type-guard

Validate before calling

if (inPlace && decimalColumn.DataType != other.DataType)
    throw new InvalidOperationException("In-place decimal ops require the other column to be decimal; use inPlace: false otherwise");

Type guard

bool SafeInPlace(DataFrameColumn left, DataFrameColumn right, bool inPlace) =>
    !inPlace || left.DataType == right.DataType;

Try / catch

try
{
    result = decimalColumn.Add(other, inPlace: true);
}
catch (ArgumentException ex) when (ex.ParamName == "column")
{
    result = decimalColumn.Add(other, inPlace: false);
}

Prevention

When it happens

Trigger: Calling an in-place binary operation API (e.g. Add(column, inPlace: true), or the InPlace variant) on a PrimitiveDataFrameColumn<decimal> with a right column whose element type is not decimal (e.g. int, double).

Common situations: Using the in-place overload for convenience while mixing int and decimal columns; migrating code that previously used the non-in-place overload to in-place form.

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