dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(Int64))
What it means
DataFrameColumnCollection.GetInt64Column(name) only returns a column when it is exactly an Int64DataFrameColumn. 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(Int64). 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:410
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int32)));
}
/// <summary>
/// Gets the <see cref="Int64DataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="Int64DataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public Int64DataFrameColumn GetInt64Column(string name)
{
DataFrameColumn column = this[name];
if (column is Int64DataFrameColumn ret)
{
return ret;
}
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)));
}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 long before access: df.Columns["name"] = df.Columns["name"].Cast<long>(), then call GetInt64Column.
- 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 long.
Example fix
// before
var col = df.Columns.GetInt64Column("Id"); // throws if "Id" is int
// after
if (df.Columns["Id"].DataType == typeof(long))
{
var col = df.Columns.GetInt64Column("Id");
}
else
{
df.Columns["Id"] = df.Columns["Id"].Cast<long>();
var col = df.Columns.GetInt64Column("Id");
} Defensive patterns
Strategy: type-guard
Validate before calling
// before calling GetInt64Column
if (df.Columns.Contains("name") && df.Columns["name"].DataType != typeof(long))
throw new InvalidOperationException($"Column 'name' is {df.Columns["name"].DataType}, expected long"); Type guard
static bool IsInt64Column(DataFrameColumn c) => c is Int64DataFrameColumn;
// usage: if (df.Columns["name"] is Int64DataFrameColumn longCol) { ... } Try / catch
try
{
var col = df.Columns.GetInt64Column("name");
}
catch (ArgumentException ex) when (ex.Message.Contains("column"))
{
// inspect df.Columns["name"].DataType, convert with .Cast<long>(), 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<long>() when column types come from external data.
- ID and timestamp-like columns may load as Int32 or double; convert to long explicitly after load.
- Log the loaded schema after reading external data to catch inference surprises early.
When it happens
Trigger: Calling df.Columns.GetInt64Column("name") on a DataFrame whose column "name" exists but is not an Int64DataFrameColumn (e.g. it was loaded as Int32, double, or string). The indexer this[name] succeeds, the `is Int64DataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:410 is thrown.
Common situations: ID/timestamp columns loaded as Int32 or double; DB BIGINT mapped differently; schema drift after a data source change; 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/04f04b0f5a6a09ab.
Report an issue: GitHub.