{"record":{"id":"4e505e0be2c91c90","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-4e505e","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(Byte))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(Byte\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":308,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Boolean)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"ByteDataFrameColumn\"/> with the specified <paramref name=\"name\"/> and attempts to return it as an <see cref=\"ByteDataFrameColumn\"/>. If <see cref=\"DataFrameColumn.DataType\"/> is not of type <see cref=\"Byte\"/>, an exception is thrown.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"ByteDataFrameColumn\"/>.</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 ByteDataFrameColumn GetByteColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is ByteDataFrameColumn ret)\n            {\n                return ret;\n            }\n\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        }","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L290-L326","documentation":"DataFrameColumnCollection.GetByteColumn(name) only returns a column when it is exactly a ByteDataFrameColumn. When a column exists under that name but has a different CLR/data type, the library throws ArgumentException with Strings.BadColumnCast, formatted with the actual column DataType and typeof(Byte). It signals a type mismatch between the requested accessor and the stored column, not a missing column.","triggerScenarios":"Calling df.Columns.GetByteColumn(\"name\") on a DataFrame whose column \"name\" exists but is not a ByteDataFrameColumn (e.g. it was created from int, double, or string data). The indexer this[name] succeeds, the `is ByteDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:308 is thrown.","commonSituations":"Loading CSV/parquet data where the column was inferred as Int32 or Double instead of byte; schema drift after a data source change; assuming column order or inference rules that produced a different primitive 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 byte before access: df[\"name\"] = df[\"name\"].Cast<byte>() (or create a new ByteDataFrameColumn from converted values), then call GetByteColumn.","If the type can vary, access generically via df[\"name\"] and inspect/convert values instead of using the typed accessor.","Fix upstream data ingestion (e.g. schema/read options) so the column is loaded as byte."],"exampleFix":"// before\nvar col = df.Columns.GetByteColumn(\"Flags\"); // throws if \"Flags\" is Int32\n// after\nif (df.Columns[\"Flags\"].DataType == typeof(byte))\n{\n    var col = df.Columns.GetByteColumn(\"Flags\");\n}\nelse\n{\n    df.Columns[\"Flags\"] = df.Columns[\"Flags\"].Cast<byte>();\n    var col = df.Columns.GetByteColumn(\"Flags\");\n}","handlingStrategy":"type-guard","validationCode":"// before calling GetByteColumn\nif (df.Columns.Contains(\"name\") && df.Columns[\"name\"].DataType != typeof(byte))\n    throw new InvalidOperationException($\"Column 'name' is {df.Columns[\"name\"].DataType}, expected byte\");","typeGuard":"static bool IsByteColumn(DataFrameColumn c) => c is ByteDataFrameColumn;\n// usage: if (df.Columns[\"name\"] is ByteDataFrameColumn byteCol) { ... }","tryCatchPattern":"try\n{\n    var col = df.Columns.GetByteColumn(\"name\");\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"column\"))\n{\n    // inspect df.Columns[\"name\"].DataType, convert with .Cast<byte>(), 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<T>() when column types come from external data.","Log or assert the loaded schema (column name -> DataType) right after reading CSV/parquet.","Avoid relying on CSV type inference for fixed-width columns; pass explicit read options or convert immediately after load."],"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"}