dotnet/machinelearning · error · ArgumentException

Strings.BadColumnCast (formatted with column.DataType, typeo

Error message

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

What it means

DataFrameColumnCollection.GetByteColumn(name) only returns a column when it is exactly a ByteDataFrameColumn. When a column exists under that name but has a different CLR/data type, the library throws ArgumentException with Strings.BadColumnCast, formatted with the actual column DataType and typeof(Byte). It signals a type mismatch between the requested accessor and the stored column, not a missing column.

Source

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

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

        /// <summary>
        /// Gets the <see cref="ByteDataFrameColumn"/> with the specified <paramref name="name"/> and attempts to return it as an <see cref="ByteDataFrameColumn"/>. If <see cref="DataFrameColumn.DataType"/> is not of type <see cref="Byte"/>, an exception is thrown.
        /// </summary>
        /// <param name="name">The name of the column</param>
        /// <returns><see cref="ByteDataFrameColumn"/>.</returns>
        /// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
        public ByteDataFrameColumn GetByteColumn(string name)
        {
            DataFrameColumn column = this[name];
            if (column is ByteDataFrameColumn ret)
            {
                return ret;
            }

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

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

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

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check the column's actual type first: df.Columns["name"].DataType (or the column class name) and use the matching Get<ColumnType>Column accessor.
  2. Convert the column to byte before access: df["name"] = df["name"].Cast<byte>() (or create a new ByteDataFrameColumn from converted values), then call GetByteColumn.
  3. If the type can vary, access generically via df["name"] and inspect/convert values instead of using the typed accessor.
  4. Fix upstream data ingestion (e.g. schema/read options) so the column is loaded as byte.

Example fix

// before
var col = df.Columns.GetByteColumn("Flags"); // throws if "Flags" is Int32
// after
if (df.Columns["Flags"].DataType == typeof(byte))
{
    var col = df.Columns.GetByteColumn("Flags");
}
else
{
    df.Columns["Flags"] = df.Columns["Flags"].Cast<byte>();
    var col = df.Columns.GetByteColumn("Flags");
}
Defensive patterns

Strategy: type-guard

Validate before calling

// before calling GetByteColumn
if (df.Columns.Contains("name") && df.Columns["name"].DataType != typeof(byte))
    throw new InvalidOperationException($"Column 'name' is {df.Columns["name"].DataType}, expected byte");

Type guard

static bool IsByteColumn(DataFrameColumn c) => c is ByteDataFrameColumn;
// usage: if (df.Columns["name"] is ByteDataFrameColumn byteCol) { ... }

Try / catch

try
{
    var col = df.Columns.GetByteColumn("name");
}
catch (ArgumentException ex) when (ex.Message.Contains("column"))
{
    // inspect df.Columns["name"].DataType, convert with .Cast<byte>(), and retry
}

Prevention

When it happens

Trigger: Calling df.Columns.GetByteColumn("name") on a DataFrame whose column "name" exists but is not a ByteDataFrameColumn (e.g. it was created from int, double, or string data). The indexer this[name] succeeds, the `is ByteDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:308 is thrown.

Common situations: Loading CSV/parquet data where the column was inferred as Int32 or Double instead of byte; schema drift after a data source change; assuming column order or inference rules that produced a different primitive type; hardcoded column names that point at a differently-typed column.

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