dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(Boolean))
What it means
GetBooleanColumn returns the named column as a BooleanDataFrameColumn or throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(Boolean)) when the underlying column is not boolean (e.g. a byte or int column).
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:291
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));
}
/// <summary>
/// Gets the <see cref="BooleanDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="BooleanDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public BooleanDataFrameColumn GetBooleanColumn(string name)
{
DataFrameColumn column = this[name];
if (column is BooleanDataFrameColumn ret)
{
return ret;
}
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)));
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Check df.Columns["flag"].DataType == typeof(bool) before calling.
- Convert the numeric/string column to a BooleanDataFrameColumn and add it to the DataFrame first.
- Catch the ArgumentException and fall back to a conversion path.
Example fix
// before
var flag = df.Columns.GetBooleanColumn("IsActive"); // stored as byte
// after
var bytes = df.Columns.GetPrimitiveColumn<byte>("IsActive");
var flag = new BooleanDataFrameColumn("IsActive", bytes.Select(b => b != 0));
df.Columns.Add(flag); Defensive patterns
Strategy: type-guard
Validate before calling
if (df.Columns["IsActive"].DataType != typeof(bool))
throw new InvalidOperationException($"'IsActive' is {df.Columns["IsActive"].DataType}, expected bool");
var flag = df.Columns.GetBooleanColumn("IsActive"); Type guard
bool IsBooleanColumn(DataFrameColumn c) => c is BooleanDataFrameColumn;
Try / catch
try
{
var flag = df.Columns.GetBooleanColumn(name);
}
catch (ArgumentException)
{
// convert byte/int/string column to BooleanDataFrameColumn
} Prevention
- Verify boolean flags survive ingestion as bool (CSV 'True/False', DB bit).
- Convert 0/1 or 'true/false' encodings to BooleanDataFrameColumn at load time.
- Check DataType before every typed getter.
When it happens
Trigger: df.Columns.GetBooleanColumn("flag") where the column stores byte/int/char values instead of BooleanDataFrameColumn.
Common situations: CSV 'true/false' parsed as string or 0/1 parsed as numeric, flags imported from databases as smallint, nullable boolean data stored as byte columns.
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
- String.Format(Strings.MismatchedColumnValueType, this.DataTy
- Strings.BadColumnCast (formatted with column.DataType, typeo
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/ad6ce8cd8457e616.
Report an issue: GitHub.