dotnet/machinelearning · error · ArgumentException

Strings.BadColumnCast (formatted with column.DataType, typeo

Error message

Strings.BadColumnCast (formatted with column.DataType, typeof(Decimal))

What it means

DataFrameColumnCollection.GetDecimalColumn(name) only returns a column when it is exactly a DecimalDataFrameColumn. 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(Decimal). 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:359

            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)));
        }

        /// <summary>
        /// Gets the <see cref="SingleDataFrameColumn"/> with the specified <paramref name="name"/>.
        /// </summary>
        /// <param name="name">The name of the column</param>
        /// <returns><see cref="SingleDataFrameColumn"/>.</returns>
        /// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
        public SingleDataFrameColumn GetSingleColumn(string name)
        {
            DataFrameColumn column = this[name];
            if (column is SingleDataFrameColumn ret)
            {
                return ret;
            }

            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Single)));
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check the column's actual type first: df.Columns["name"].DataType (or the column class name) and use the matching Get<ColumnType>Column accessor.
  2. Convert the column to decimal before access: df.Columns["name"] = df.Columns["name"].Cast<decimal>(), then call GetDecimalColumn.
  3. If the type can vary, access generically via df["name"] and inspect/convert values instead of using the typed accessor.
  4. Fix upstream data ingestion so the column is loaded as decimal.

Example fix

// before
var col = df.Columns.GetDecimalColumn("Amount"); // throws if "Amount" is double
// after
if (df.Columns["Amount"].DataType == typeof(decimal))
{
    var col = df.Columns.GetDecimalColumn("Amount");
}
else
{
    df.Columns["Amount"] = df.Columns["Amount"].Cast<decimal>();
    var col = df.Columns.GetDecimalColumn("Amount");
}
Defensive patterns

Strategy: type-guard

Validate before calling

// before calling GetDecimalColumn
if (df.Columns.Contains("name") && df.Columns["name"].DataType != typeof(decimal))
    throw new InvalidOperationException($"Column 'name' is {df.Columns["name"].DataType}, expected decimal");

Type guard

static bool IsDecimalColumn(DataFrameColumn c) => c is DecimalDataFrameColumn;
// usage: if (df.Columns["name"] is DecimalDataFrameColumn decCol) { ... }

Try / catch

try
{
    var col = df.Columns.GetDecimalColumn("name");
}
catch (ArgumentException ex) when (ex.Message.Contains("column"))
{
    // inspect df.Columns["name"].DataType, convert with .Cast<decimal>(), and retry
}

Prevention

When it happens

Trigger: Calling df.Columns.GetDecimalColumn("name") on a DataFrame whose column "name" exists but is not a DecimalDataFrameColumn (e.g. it was loaded as double, float, int, or string). The indexer this[name] succeeds, the `is DecimalDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:359 is thrown.

Common situations: Financial data loaded as double by CSV inference; database DECIMAL columns mapped to double or string during load; 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


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/841865e32d03b210. Report an issue: GitHub.