{"record":{"id":"1323bbe6b19c6d2f","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-1323bb","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(Single))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(Single\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":376,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Decimal)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"SingleDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"SingleDataFrameColumn\"/>.</returns>\n        /// <exception cref=\"ArgumentException\">A column named <paramref name=\"name\"/> cannot be found, or if the column's type doesn't match.</exception>\n        public SingleDataFrameColumn GetSingleColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is SingleDataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Single)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"Int32DataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"Int32DataFrameColumn\"/>.</returns>\n        /// <exception cref=\"ArgumentException\">A column named <paramref name=\"name\"/> cannot be found, or if the column's type doesn't match.</exception>\n        public Int32DataFrameColumn GetInt32Column(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is Int32DataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int32)));\n        }","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L358-L394","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the column's actual type first: df.Columns[\"name\"].DataType (or the column class name) and use the matching Get<ColumnType>Column accessor.","Convert the column to float before access: df.Columns[\"name\"] = df.Columns[\"name\"].Cast<float>(), then call GetSingleColumn.","If the type can vary, access generically via df[\"name\"] and inspect/convert values instead of using the typed accessor.","Fix upstream data ingestion so the column is loaded as float."],"exampleFix":"// before\nvar col = df.Columns.GetSingleColumn(\"Score\"); // throws if \"Score\" is double\n// after\nif (df.Columns[\"Score\"].DataType == typeof(float))\n{\n    var col = df.Columns.GetSingleColumn(\"Score\");\n}\nelse\n{\n    df.Columns[\"Score\"] = df.Columns[\"Score\"].Cast<float>();\n    var col = df.Columns.GetSingleColumn(\"Score\");\n}","handlingStrategy":"type-guard","validationCode":"// before calling GetSingleColumn\nif (df.Columns.Contains(\"name\") && df.Columns[\"name\"].DataType != typeof(float))\n    throw new InvalidOperationException($\"Column 'name' is {df.Columns[\"name\"].DataType}, expected float\");","typeGuard":"static bool IsSingleColumn(DataFrameColumn c) => c is SingleDataFrameColumn;\n// usage: if (df.Columns[\"name\"] is SingleDataFrameColumn fltCol) { ... }","tryCatchPattern":"try\n{\n    var col = df.Columns.GetSingleColumn(\"name\");\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"column\"))\n{\n    // inspect df.Columns[\"name\"].DataType, convert with .Cast<float>(), and retry\n}","preventionTips":["Always check df.Columns[name].DataType before using a typed Get*Column accessor.","Prefer generic access via df[\"name\"] plus explicit .Cast<float>() when column types come from external data.","ML feature columns often load as double; convert to float before float-typed access.","Log the loaded schema after reading external data to catch inference surprises early."],"tags":["dotnet","dataframe","type-mismatch","argument-exception"],"backgroundTag":"type-mismatch","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}