{"record":{"id":"12a0189ce664c071","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-12a018","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(SByte))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(SByte\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":427,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int64)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"SByteDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"SByteDataFrameColumn\"/>.</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 SByteDataFrameColumn GetSByteColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is SByteDataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(SByte)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"Int16DataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"Int16DataFrameColumn\"/>.</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 Int16DataFrameColumn GetInt16Column(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is Int16DataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int16)));\n        }","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L409-L445","documentation":"DataFrameColumnCollection.GetSByteColumn(name) only returns a column when it is exactly an SByteDataFrameColumn. 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(SByte). It signals a type mismatch between the requested accessor and the stored column, not a missing column.","triggerScenarios":"Calling df.Columns.GetSByteColumn(\"name\") on a DataFrame whose column \"name\" exists but is not an SByteDataFrameColumn (e.g. it was loaded as byte, short, int, or string). The indexer this[name] succeeds, the `is SByteDataFrameColumn` pattern fails, and the ArgumentException at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:427 is thrown.","commonSituations":"Signed small-int data inferred as byte or Int16; ML feature columns loaded as wider integers; 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 sbyte before access: df.Columns[\"name\"] = df.Columns[\"name\"].Cast<sbyte>(), then call GetSByteColumn.","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 sbyte."],"exampleFix":"// before\nvar col = df.Columns.GetSByteColumn(\"Delta\"); // throws if \"Delta\" is short\n// after\nif (df.Columns[\"Delta\"].DataType == typeof(sbyte))\n{\n    var col = df.Columns.GetSByteColumn(\"Delta\");\n}\nelse\n{\n    df.Columns[\"Delta\"] = df.Columns[\"Delta\"].Cast<sbyte>();\n    var col = df.Columns.GetSByteColumn(\"Delta\");\n}","handlingStrategy":"type-guard","validationCode":"// before calling GetSByteColumn\nif (df.Columns.Contains(\"name\") && df.Columns[\"name\"].DataType != typeof(sbyte))\n    throw new InvalidOperationException($\"Column 'name' is {df.Columns[\"name\"].DataType}, expected sbyte\");","typeGuard":"static bool IsSByteColumn(DataFrameColumn c) => c is SByteDataFrameColumn;\n// usage: if (df.Columns[\"name\"] is SByteDataFrameColumn sbCol) { ... }","tryCatchPattern":"try\n{\n    var col = df.Columns.GetSByteColumn(\"name\");\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"column\"))\n{\n    // inspect df.Columns[\"name\"].DataType, convert with .Cast<sbyte>(), 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<sbyte>() when column types come from external data.","Don't confuse byte and sbyte columns; signed and unsigned byte columns are distinct types here.","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"}