dotnet/machinelearning · error · ArgumentException

Strings.BadColumnCast (formatted with column.DataType, typeo

Error message

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

What it means

DataFrameColumnCollection.GetSingleColumn(name) only returns a column when it is exactly a SingleDataFrameColumn (System.Single, i.e. float). 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(Single). 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:376

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

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

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

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 float before access: df.Columns["name"] = df.Columns["name"].Cast<float>(), then call GetSingleColumn.
  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 float.

Example fix

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

Strategy: type-guard

Validate before calling

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

Type guard

static bool IsSingleColumn(DataFrameColumn c) => c is SingleDataFrameColumn;
// usage: if (df.Columns["name"] is SingleDataFrameColumn fltCol) { ... }

Try / catch

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

Prevention

When it happens

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

Common situations: Numeric CSV data inferred as double or Int64 instead of float; ML pipelines assuming float32 features but loaders produced double; schema drift; 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/1323bbe6b19c6d2f. Report an issue: GitHub.