dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(SByte))
What it means
DataFrameColumnCollection.GetSByteColumn(name) only returns a column when it is exactly an SByteDataFrameColumn. When a column exists under that name but has a different data type, the library throws ArgumentException with Strings.BadColumnCast, formatted with the actual column DataType and typeof(SByte). 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:427
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int64)));
}
/// <summary>
/// Gets the <see cref="SByteDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="SByteDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public SByteDataFrameColumn GetSByteColumn(string name)
{
DataFrameColumn column = this[name];
if (column is SByteDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(SByte)));
}
/// <summary>
/// Gets the <see cref="Int16DataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="Int16DataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public Int16DataFrameColumn GetInt16Column(string name)
{
DataFrameColumn column = this[name];
if (column is Int16DataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int16)));
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Check the column's actual type first: df.Columns["name"].DataType (or the column class name) and use the matching Get<ColumnType>Column accessor.
- Convert the column to sbyte before access: df.Columns["name"] = df.Columns["name"].Cast<sbyte>(), then call GetSByteColumn.
- If the type can vary, access generically via df["name"] and inspect/convert values instead of using the typed accessor.
- Fix upstream data ingestion so the column is loaded as sbyte.
Example fix
// before
var col = df.Columns.GetSByteColumn("Delta"); // throws if "Delta" is short
// after
if (df.Columns["Delta"].DataType == typeof(sbyte))
{
var col = df.Columns.GetSByteColumn("Delta");
}
else
{
df.Columns["Delta"] = df.Columns["Delta"].Cast<sbyte>();
var col = df.Columns.GetSByteColumn("Delta");
} Defensive patterns
Strategy: type-guard
Validate before calling
// before calling GetSByteColumn
if (df.Columns.Contains("name") && df.Columns["name"].DataType != typeof(sbyte))
throw new InvalidOperationException($"Column 'name' is {df.Columns["name"].DataType}, expected sbyte"); Type guard
static bool IsSByteColumn(DataFrameColumn c) => c is SByteDataFrameColumn;
// usage: if (df.Columns["name"] is SByteDataFrameColumn sbCol) { ... } Try / catch
try
{
var col = df.Columns.GetSByteColumn("name");
}
catch (ArgumentException ex) when (ex.Message.Contains("column"))
{
// inspect df.Columns["name"].DataType, convert with .Cast<sbyte>(), and retry
} Prevention
- Always check df.Columns[name].DataType before using a typed Get*Column accessor.
- Prefer generic access via df["name"] plus explicit .Cast<sbyte>() when column types come from external data.
- Don't confuse byte and sbyte columns; signed and unsigned byte columns are distinct types here.
- Log the loaded schema after reading external data to catch inference surprises early.
When it happens
Trigger: Calling df.Columns.GetSByteColumn("name") on a DataFrame whose column "name" exists but is not an SByteDataFrameColumn (e.g. it was loaded as byte, short, int, or string). The indexer this[name] succeeds, the `is SByteDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:427 is thrown.
Common situations: Signed small-int data inferred as byte or Int16; ML feature columns loaded as wider integers; schema drift; hardcoded column names pointing 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
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/12a0189ce664c071.
Report an issue: GitHub.