dotnet/machinelearning · error · ArgumentException

Strings.BadColumnCast (formatted with column.DataType, typeo

Error message

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

What it means

GetUInt64Column looks up a column by name and returns it only if it is a UInt64DataFrameColumn. If the column exists but has a different element type, the library throws ArgumentException with Strings.BadColumnCast, formatted with the column's actual DataType and typeof(UInt64). This is a deliberate fail-fast instead of a silent invalid cast. Note it also throws if the name is absent (column is null).

Source

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

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

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

            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. Check the column type before calling: if (df.Columns[name].DataType == typeof(ulong)) df.Columns.GetUInt64Column(name);
  2. Use the generic indexer or Convert: df[name] as UInt64DataFrameColumn, or create a converted column via column.Clone / arithmetic conversion APIs if a cast is genuinely needed.
  3. Fix the data loading/parsing so the column is created as UInt64DataFrameColumn (explicit schema when reading files).
  4. Verify the column name spelling and that the intended column exists (this[name] returning null also leads to a throw here).

Example fix

// before
var col = df.Columns.GetUInt64Column("Id"); // throws if inferred as Int64

// after
if (df.Columns["Id"].DataType != typeof(ulong))
{
    df["Id"] = df["Id"].Cast<ulong>(); // or load with explicit schema
}
var col = df.Columns.GetUInt64Column("Id");
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

static bool IsUInt64Column(DataFrameColumn c) => c is UInt64DataFrameColumn;

Try / catch

try { var col = df.Columns.GetUInt64Column(name); }
catch (ArgumentException ex) { /* handle wrong/missing column type */ }

Prevention

When it happens

Trigger: Calling DataFrameColumnCollection.GetUInt64Column(name) where the column named 'name' exists but is not a UInt64DataFrameColumn (e.g. it was created as Int64, Double, or String), or where no column with that name exists.

Common situations: Reading CSV/JSON data where the column was inferred as Int32/Int64/Double and the code assumes UInt64; schema drift after a data source change; typos in column names that silently resolve to a differently-typed column; mixed-type dataframes built programmatically.

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