dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(Double))
What it means
DataFrameColumnCollection.GetDoubleColumn(name) only returns a column when it is exactly a DoubleDataFrameColumn. 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(Double). 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:342
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)));
}
/// <summary>
/// Gets the <see cref="DecimalDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="DecimalDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public DecimalDataFrameColumn GetDecimalColumn(string name)
{
DataFrameColumn column = this[name];
if (column is DecimalDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Decimal)));
}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 double before access: df.Columns["name"] = df.Columns["name"].Cast<double>(), then call GetDoubleColumn.
- If the type can vary, access generically via df["name"] and inspect/convert values instead of using the typed accessor.
- Fix upstream data ingestion (read options/schema) so the column is loaded as double.
Example fix
// before
var col = df.Columns.GetDoubleColumn("Price"); // throws if "Price" is float
// after
if (df.Columns["Price"].DataType == typeof(double))
{
var col = df.Columns.GetDoubleColumn("Price");
}
else
{
df.Columns["Price"] = df.Columns["Price"].Cast<double>();
var col = df.Columns.GetDoubleColumn("Price");
} Defensive patterns
Strategy: type-guard
Validate before calling
// before calling GetDoubleColumn
if (df.Columns.Contains("name") && df.Columns["name"].DataType != typeof(double))
throw new InvalidOperationException($"Column 'name' is {df.Columns["name"].DataType}, expected double"); Type guard
static bool IsDoubleColumn(DataFrameColumn c) => c is DoubleDataFrameColumn;
// usage: if (df.Columns["name"] is DoubleDataFrameColumn dblCol) { ... } Try / catch
try
{
var col = df.Columns.GetDoubleColumn("name");
}
catch (ArgumentException ex) when (ex.Message.Contains("column"))
{
// inspect df.Columns["name"].DataType, convert with .Cast<double>(), 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<double>() when column types come from external data.
- Watch for float/decimal columns from numeric CSV or DB REAL/NUMERIC types; convert immediately after load.
- Log the loaded schema after reading external data to catch inference surprises early.
When it happens
Trigger: Calling df.Columns.GetDoubleColumn("name") on a DataFrame whose column "name" exists but is not a DoubleDataFrameColumn (e.g. it was loaded as float, decimal, int, or string). The indexer this[name] succeeds, the `is DoubleDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:342 is thrown.
Common situations: CSV values inferred as float or Int64 instead of double; parquet/DB schemas with REAL or NUMERIC mapping to single/decimal; 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/00fbb5fd780b8686.
Report an issue: GitHub.