dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(string))
What it means
GetArrowStringColumn returns the named column as an ArrowStringDataFrameColumn or throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(string)) if it is a different column kind (commonly a StringDataFrameColumn).
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:257
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(DateTime)));
}
/// <summary>
/// Gets the <see cref="ArrowStringDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="ArrowStringDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public ArrowStringDataFrameColumn GetArrowStringColumn(string name)
{
DataFrameColumn column = this[name];
if (column is ArrowStringDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));
}
/// <summary>
/// Gets the <see cref="StringDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="StringDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public StringDataFrameColumn GetStringColumn(string name)
{
DataFrameColumn column = this[name];
if (column is StringDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Check whether the column 'is ArrowStringDataFrameColumn' before calling.
- If a StringDataFrameColumn is what you have, call GetStringColumn instead, or convert to ArrowStringDataFrameColumn.
- Normalize string column storage at load time so accessors match.
Example fix
// before
var s = df.Columns.GetArrowStringColumn("Name"); // Name is StringDataFrameColumn
// after
var s = df.Columns["Name"] is ArrowStringDataFrameColumn arrow
? arrow
: df.Columns.GetStringColumn("Name"); Defensive patterns
Strategy: type-guard
Validate before calling
var c = df.Columns["Name"];
var s = c is ArrowStringDataFrameColumn
? df.Columns.GetArrowStringColumn("Name")
: throw new InvalidOperationException($"'Name' is {c.DataType}, not arrow string"); Type guard
bool IsArrowString(DataFrameColumn c) => c is ArrowStringDataFrameColumn;
Try / catch
try
{
var s = df.Columns.GetArrowStringColumn(name);
}
catch (ArgumentException)
{
// fall back to GetStringColumn or convert storage
} Prevention
- Know which ingestion path produces ArrowString vs String columns.
- Normalize string column storage right after loading.
- Use pattern matching instead of assuming string column kind.
When it happens
Trigger: df.Columns.GetArrowStringColumn("s") where the column is a StringDataFrameColumn or another non-arrow string storage.
Common situations: Columns built in memory as StringDataFrameColumn but fetched as ArrowString, data read from formats that map strings differently (CSV vs Arrow files).
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/7e5137f9bcb97a0f.
Report an issue: GitHub.