{"record":{"id":"ad6ce8cd8457e616","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-ad6ce8","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(Boolean))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(Boolean\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":291,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"BooleanDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"BooleanDataFrameColumn\"/>.</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 BooleanDataFrameColumn GetBooleanColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is BooleanDataFrameColumn ret)\n            {\n                return ret;\n            }\n\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        }","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L273-L309","documentation":"GetBooleanColumn returns the named column as a BooleanDataFrameColumn or throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(Boolean)) when the underlying column is not boolean (e.g. a byte or int column).","triggerScenarios":"df.Columns.GetBooleanColumn(\"flag\") where the column stores byte/int/char values instead of BooleanDataFrameColumn.","commonSituations":"CSV 'true/false' parsed as string or 0/1 parsed as numeric, flags imported from databases as smallint, nullable boolean data stored as byte columns.","solutions":["Check df.Columns[\"flag\"].DataType == typeof(bool) before calling.","Convert the numeric/string column to a BooleanDataFrameColumn and add it to the DataFrame first.","Catch the ArgumentException and fall back to a conversion path."],"exampleFix":"// before\nvar flag = df.Columns.GetBooleanColumn(\"IsActive\"); // stored as byte\n// after\nvar bytes = df.Columns.GetPrimitiveColumn<byte>(\"IsActive\");\nvar flag = new BooleanDataFrameColumn(\"IsActive\", bytes.Select(b => b != 0));\ndf.Columns.Add(flag);","handlingStrategy":"type-guard","validationCode":"if (df.Columns[\"IsActive\"].DataType != typeof(bool))\n    throw new InvalidOperationException($\"'IsActive' is {df.Columns[\"IsActive\"].DataType}, expected bool\");\nvar flag = df.Columns.GetBooleanColumn(\"IsActive\");","typeGuard":"bool IsBooleanColumn(DataFrameColumn c) => c is BooleanDataFrameColumn;","tryCatchPattern":"try\n{\n    var flag = df.Columns.GetBooleanColumn(name);\n}\ncatch (ArgumentException)\n{\n    // convert byte/int/string column to BooleanDataFrameColumn\n}","preventionTips":["Verify boolean flags survive ingestion as bool (CSV 'True/False', DB bit).","Convert 0/1 or 'true/false' encodings to BooleanDataFrameColumn at load time.","Check DataType before every typed getter."],"tags":["dataframe","bad-cast","type-mismatch","dotnet"],"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"}