dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(Char))
What it means
DataFrameColumnCollection.GetCharColumn(name) only returns a column when it is exactly a CharDataFrameColumn. 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(Char). 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:325
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)));
}
/// <summary>
/// Gets the <see cref="DoubleDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="DoubleDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public DoubleDataFrameColumn GetDoubleColumn(string name)
{
DataFrameColumn column = this[name];
if (column is DoubleDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Double)));
}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 char before access: df.Columns["name"] = df.Columns["name"].Cast<char>() (or build a CharDataFrameColumn from converted values), then call GetCharColumn.
- 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 char.
Example fix
// before
var col = df.Columns.GetCharColumn("Initial"); // throws if "Initial" is string
// after
if (df.Columns["Initial"] is CharDataFrameColumn)
{
var col = df.Columns.GetCharColumn("Initial");
}
else
{
df.Columns["Initial"] = df.Columns["Initial"].Cast<char>();
var col = df.Columns.GetCharColumn("Initial");
} Defensive patterns
Strategy: type-guard
Validate before calling
// before calling GetCharColumn
if (df.Columns.Contains("name") && df.Columns["name"].DataType != typeof(char))
throw new InvalidOperationException($"Column 'name' is {df.Columns["name"].DataType}, expected char"); Type guard
static bool IsCharColumn(DataFrameColumn c) => c is CharDataFrameColumn;
// usage: if (df.Columns["name"] is CharDataFrameColumn charCol) { ... } Try / catch
try
{
var col = df.Columns.GetCharColumn("name");
}
catch (ArgumentException ex) when (ex.Message.Contains("column"))
{
// inspect df.Columns["name"].DataType, convert with .Cast<char>(), 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<char>() when column types come from external data.
- Remember string data loads as StringDataFrameColumn, not char; convert deliberately.
- Log the loaded schema after reading external data to catch inference surprises early.
When it happens
Trigger: Calling df.Columns.GetCharColumn("name") on a DataFrame whose column "name" exists but is not a CharDataFrameColumn (e.g. it was created from string, int, or float data). The indexer this[name] succeeds, the `is CharDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:325 is thrown.
Common situations: Text data loaded as StringDataFrameColumn while code expects char; schema drift after a data source change; assuming column inference rules that produced a different 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
- 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/7a48e3cbfe419558.
Report an issue: GitHub.