dotnet/machinelearning · error · ArgumentException

String.Format(Strings.MismatchedColumnValueType, this.DataTy

Error message

String.Format(Strings.MismatchedColumnValueType, this.DataType)

What it means

GetGroupedOccurrences computes how row indices of `this` column map to indices of equal values in an `other` column, by hashing `other` with GroupColumnValues<TKey>. The operation is only defined when both columns hold the same element type, so it validates DataType equality up front and throws ArgumentException naming the `other` parameter. This mirrors how binary column operations elsewhere in Microsoft.Data.Analysis require compatible types.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumn.cs:275

        /// <summary>
        /// Get occurences of each value from this column in other column, grouped by this value
        /// </summary>
        /// <param name="other"></param>
        /// <param name="otherColumnNullIndices"></param>
        /// <returns>A mapping of index from this column to the indices of same value in other column</returns>
        public abstract Dictionary<long, ICollection<long>> GetGroupedOccurrences(DataFrameColumn other, out HashSet<long> otherColumnNullIndices);

        /// <summary>
        /// Get occurences of each value from this column in other column, grouped by this value
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="other"></param>
        /// <param name="otherColumnNullIndices"></param>
        /// <returns>A mapping of index from this column to the indices of same value in other column</returns>
        protected Dictionary<long, ICollection<long>> GetGroupedOccurrences<TKey>(DataFrameColumn other, out HashSet<long> otherColumnNullIndices)
        {
            if (this.DataType != other.DataType)
                throw new ArgumentException(String.Format(Strings.MismatchedColumnValueType, this.DataType), nameof(other));

            // First hash other column   
            Dictionary<TKey, ICollection<long>> multimap = other.GroupColumnValues<TKey>(out otherColumnNullIndices);

            var ret = new Dictionary<long, ICollection<long>>();

            //For each value in this column find rows from other column with equal value
            for (int i = 0; i < this.Length; i++)
            {
                var value = this[i];
                if (value != null && multimap.TryGetValue((TKey)value, out ICollection<long> otherRowIndices))
                {
                    ret.Add(i, otherRowIndices);
                }
            }

            return ret;
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure both columns have the same DataType before the operation (compare column.DataType).
  2. Explicitly cast/convert one column to the other's type (e.g. create a new PrimitiveDataFrameColumn<T> of the target type and populate it, or re-read the data with a consistent schema).
  3. When loading data, pass explicit read options/schema so the same logical column is typed identically in both DataFrames.

Example fix

// before
df1.Merge<int>(df2, leftKey, rightKey); // int vs double columns -> ArgumentException
// after
var leftCol = (PrimitiveDataFrameColumn<int>)df1.Columns[leftKey];
var rightColDouble = (PrimitiveDataFrameColumn<double>)df2.Columns[rightKey];
var rightCol = rightColDouble.Select(v => (int)v).ToColumn(rightKey); // match types first
Defensive patterns

Strategy: validation

Validate before calling

if (leftCol.DataType != rightCol.DataType)
    throw new InvalidOperationException($"Column types differ: {leftCol.DataType} vs {rightCol.DataType}; convert first.");

Type guard

static bool SameTypedColumns(DataFrameColumn a, DataFrameColumn b) => a?.DataType == b?.DataType;

Try / catch

try
{
    MergeColumns(left, right);
}
catch (ArgumentException ex) when (ex.ParamName == "other" && ex.Message.Contains("type"))
{
    // fall back: convert right column to left.DataType and retry
}

Prevention

When it happens

Trigger: Calling a public API that internally calls GetGroupedOccurrences (e.g. column comparison/alignment operations used by DataFrame joins and binary operations) with two columns whose DataType differ, such as PrimitiveDataFrameColumn<int> vs PrimitiveDataFrameColumn<double> or a string column vs a numeric column.

Common situations: Joining or comparing DataFrames read from different sources where one parsed a column as int and the other as long/double; mixing a StringDataFrameColumn with a PrimitiveDataFrameColumn<T>; schema drift after a CSV schema inference change.

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