dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(DateTime))
What it means
GetDateTimeColumn returns the named column as a DateTimeDataFrameColumn or throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(DateTime)) when the column's type differs. Note this throw omits a paramName, unlike GetPrimitiveColumn.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:240
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(T)), nameof(T));
}
/// <summary>
/// Gets the <see cref="DateTimeDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="DateTimeDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public DateTimeDataFrameColumn GetDateTimeColumn(string name)
{
DataFrameColumn column = this[name];
if (column is DateTimeDataFrameColumn ret)
{
return ret;
}
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)));
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Verify column.DataType == typeof(DateTime) before calling, or catch and convert.
- Parse the underlying column into a new DateTimeDataFrameColumn first, add it, then fetch it.
- Inspect the actual column kind via pattern matching (is DateTimeDataFrameColumn).
Example fix
// before
var ts = df.Columns.GetDateTimeColumn("ts"); // ts is string column
// after
var tsStr = df.Columns.GetArrowStringColumn("ts");
var ts = new DateTimeDataFrameColumn("ts", tsStr.Select(DateTime.Parse));
df.Columns.Add(ts); Defensive patterns
Strategy: type-guard
Validate before calling
if (!(df.Columns["ts"] is DateTimeDataFrameColumn))
throw new InvalidOperationException($"'ts' is {df.Columns["ts"].DataType}, not DateTime");
var ts = df.Columns.GetDateTimeColumn("ts"); Type guard
bool IsDateTimeColumn(DataFrameColumn c) => c is DateTimeDataFrameColumn;
Try / catch
try
{
var ts = df.Columns.GetDateTimeColumn(name);
}
catch (ArgumentException)
{
// convert/parse the actual column type into DateTimeDataFrameColumn
} Prevention
- Confirm date columns are stored as DateTimeDataFrameColumn after ingestion, not string/long.
- Normalize timestamp columns at load time.
- Pattern-match column kind before typed getters.
When it happens
Trigger: df.Columns.GetDateTimeColumn("ts") where the column is e.g. a string or PrimitiveDataFrameColumn, not DateTimeDataFrameColumn.
Common situations: Date columns parsed as strings from CSV, timestamp columns stored as long (unix epoch), timezone columns of differing storage types.
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/9b23b89545b536a0.
Report an issue: GitHub.