{"record":{"id":"00fbb5fd780b8686","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-00fbb5","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(Double))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(Double\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":342,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Char)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"DoubleDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"DoubleDataFrameColumn\"/>.</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 DoubleDataFrameColumn GetDoubleColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is DoubleDataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Double)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"DecimalDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"DecimalDataFrameColumn\"/>.</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 DecimalDataFrameColumn GetDecimalColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is DecimalDataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Decimal)));\n        }","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L324-L360","documentation":"DataFrameColumnCollection.GetDoubleColumn(name) only returns a column when it is exactly a DoubleDataFrameColumn. 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(Double). It signals a type mismatch between the requested accessor and the stored column, not a missing column.","triggerScenarios":"Calling df.Columns.GetDoubleColumn(\"name\") on a DataFrame whose column \"name\" exists but is not a DoubleDataFrameColumn (e.g. it was loaded as float, decimal, int, or string). The indexer this[name] succeeds, the `is DoubleDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:342 is thrown.","commonSituations":"CSV values inferred as float or Int64 instead of double; parquet/DB schemas with REAL or NUMERIC mapping to single/decimal; schema drift after a data source change; 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 double before access: df.Columns[\"name\"] = df.Columns[\"name\"].Cast<double>(), then call GetDoubleColumn.","If the type can vary, access generically via df[\"name\"] and inspect/convert values instead of using the typed accessor.","Fix upstream data ingestion (read options/schema) so the column is loaded as double."],"exampleFix":"// before\nvar col = df.Columns.GetDoubleColumn(\"Price\"); // throws if \"Price\" is float\n// after\nif (df.Columns[\"Price\"].DataType == typeof(double))\n{\n    var col = df.Columns.GetDoubleColumn(\"Price\");\n}\nelse\n{\n    df.Columns[\"Price\"] = df.Columns[\"Price\"].Cast<double>();\n    var col = df.Columns.GetDoubleColumn(\"Price\");\n}","handlingStrategy":"type-guard","validationCode":"// before calling GetDoubleColumn\nif (df.Columns.Contains(\"name\") && df.Columns[\"name\"].DataType != typeof(double))\n    throw new InvalidOperationException($\"Column 'name' is {df.Columns[\"name\"].DataType}, expected double\");","typeGuard":"static bool IsDoubleColumn(DataFrameColumn c) => c is DoubleDataFrameColumn;\n// usage: if (df.Columns[\"name\"] is DoubleDataFrameColumn dblCol) { ... }","tryCatchPattern":"try\n{\n    var col = df.Columns.GetDoubleColumn(\"name\");\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"column\"))\n{\n    // inspect df.Columns[\"name\"].DataType, convert with .Cast<double>(), 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<double>() when column types come from external data.","Watch for float/decimal columns from numeric CSV or DB REAL/NUMERIC types; convert immediately after load.","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"}