{"record":{"id":"7a48e3cbfe419558","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-7a48e3","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(Char))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(Char\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":325,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Byte)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"CharDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"CharDataFrameColumn\"/>.</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 CharDataFrameColumn GetCharColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is CharDataFrameColumn ret)\n            {\n                return ret;\n            }\n\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        }","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L307-L343","documentation":"DataFrameColumnCollection.GetCharColumn(name) only returns a column when it is exactly a CharDataFrameColumn. 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(Char). It signals a type mismatch between the requested accessor and the stored column, not a missing column.","triggerScenarios":"Calling df.Columns.GetCharColumn(\"name\") on a DataFrame whose column \"name\" exists but is not a CharDataFrameColumn (e.g. it was created from string, int, or float data). The indexer this[name] succeeds, the `is CharDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:325 is thrown.","commonSituations":"Text data loaded as StringDataFrameColumn while code expects char; schema drift after a data source change; assuming column inference rules that produced a different type; hardcoded column names that point 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 char before access: df.Columns[\"name\"] = df.Columns[\"name\"].Cast<char>() (or build a CharDataFrameColumn from converted values), then call GetCharColumn.","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 char."],"exampleFix":"// before\nvar col = df.Columns.GetCharColumn(\"Initial\"); // throws if \"Initial\" is string\n// after\nif (df.Columns[\"Initial\"] is CharDataFrameColumn)\n{\n    var col = df.Columns.GetCharColumn(\"Initial\");\n}\nelse\n{\n    df.Columns[\"Initial\"] = df.Columns[\"Initial\"].Cast<char>();\n    var col = df.Columns.GetCharColumn(\"Initial\");\n}","handlingStrategy":"type-guard","validationCode":"// before calling GetCharColumn\nif (df.Columns.Contains(\"name\") && df.Columns[\"name\"].DataType != typeof(char))\n    throw new InvalidOperationException($\"Column 'name' is {df.Columns[\"name\"].DataType}, expected char\");","typeGuard":"static bool IsCharColumn(DataFrameColumn c) => c is CharDataFrameColumn;\n// usage: if (df.Columns[\"name\"] is CharDataFrameColumn charCol) { ... }","tryCatchPattern":"try\n{\n    var col = df.Columns.GetCharColumn(\"name\");\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"column\"))\n{\n    // inspect df.Columns[\"name\"].DataType, convert with .Cast<char>(), 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<char>() when column types come from external data.","Remember string data loads as StringDataFrameColumn, not char; convert deliberately.","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"}