dotnet/machinelearning · error · ArgumentException

Strings.BadColumnCast (formatted with column.DataType, typeo

Error message

Strings.BadColumnCast (formatted with column.DataType, typeof(UInt16))

What it means

GetUInt16Column retrieves a column by name and requires it to be a UInt16DataFrameColumn; otherwise it throws ArgumentException with Strings.BadColumnCast formatted with the actual DataType and typeof(UInt16). The library refuses to implicitly cast between column types to avoid silent data corruption. A missing name yields the same throw path (column is null).

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:495

            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt64)));
        }

        /// <summary>
        /// Gets the <see cref="UInt16DataFrameColumn"/> with the specified <paramref name="name"/>.
        /// </summary>
        /// <param name="name">The name of the column</param>
        /// <returns><see cref="UInt16DataFrameColumn"/>.</returns>
        /// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
        public UInt16DataFrameColumn GetUInt16Column(string name)
        {
            DataFrameColumn column = this[name];
            if (column is UInt16DataFrameColumn ret)
            {
                return ret;
            }

            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt16)));
        }
    }
}

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Guard on type first: if (df.Columns[name] is UInt16DataFrameColumn col) ... else handle the mismatch.
  2. Convert the column explicitly (e.g. build a new UInt16DataFrameColumn from converted values) instead of relying on GetUInt16Column.
  3. Load data with an explicit schema so the column materializes as UInt16DataFrameColumn.
  4. Confirm the column name; a wrong or missing name also results in this throw.

Example fix

// before
var col = df.Columns.GetUInt16Column("Flags"); // throws: column is Int32

// after
if (df.Columns["Flags"] is UInt16DataFrameColumn col)
{
    // use col
}
else
{
    df["Flags"] = new UInt16DataFrameColumn("Flags", df.Rows.Count); // populate via conversion
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (df.Columns[name] is not UInt16DataFrameColumn)
    throw new InvalidOperationException($"Column '{name}' is {df.Columns[name]?.DataType?.Name ?? "missing"}, expected UInt16");

Type guard

static bool IsUInt16Column(DataFrameColumn c) => c is UInt16DataFrameColumn;

Try / catch

try { var col = df.Columns.GetUInt16Column(name); }
catch (ArgumentException ex) { /* fall back to conversion path */ }

Prevention

When it happens

Trigger: Calling DataFrameColumnCollection.GetUInt16Column(name) when the column named 'name' is of another type (Int32, Int64, Single, String, etc.) or the name does not exist in the collection.

Common situations: Inferred column types from CSV/DB import differing from expected ushort; assuming numeric columns are interchangeable; copy-pasted accessor code using the wrong getter for the column's real type.

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