{"record":{"id":"ad738692cfa0b9f4","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-ad7386","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(UInt16))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(UInt16\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":495,"sourceCode":"\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt64)));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"UInt16DataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"UInt16DataFrameColumn\"/>.</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 UInt16DataFrameColumn GetUInt16Column(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is UInt16DataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt16)));\n        }\n    }\n}\n","sourceCodeStart":477,"sourceCodeEnd":499,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L477-L499","documentation":"GetUInt16Column retrieves a column by name and requires it to be a UInt16DataFrameColumn; otherwise it throws ArgumentException with Strings.BadColumnCast formatted with the actual DataType and typeof(UInt16). The library refuses to implicitly cast between column types to avoid silent data corruption. A missing name yields the same throw path (column is null).","triggerScenarios":"Calling DataFrameColumnCollection.GetUInt16Column(name) when the column named 'name' is of another type (Int32, Int64, Single, String, etc.) or the name does not exist in the collection.","commonSituations":"Inferred column types from CSV/DB import differing from expected ushort; assuming numeric columns are interchangeable; copy-pasted accessor code using the wrong getter for the column's real type.","solutions":["Guard on type first: if (df.Columns[name] is UInt16DataFrameColumn col) ... else handle the mismatch.","Convert the column explicitly (e.g. build a new UInt16DataFrameColumn from converted values) instead of relying on GetUInt16Column.","Load data with an explicit schema so the column materializes as UInt16DataFrameColumn.","Confirm the column name; a wrong or missing name also results in this throw."],"exampleFix":"// before\nvar col = df.Columns.GetUInt16Column(\"Flags\"); // throws: column is Int32\n\n// after\nif (df.Columns[\"Flags\"] is UInt16DataFrameColumn col)\n{\n    // use col\n}\nelse\n{\n    df[\"Flags\"] = new UInt16DataFrameColumn(\"Flags\", df.Rows.Count); // populate via conversion\n}","handlingStrategy":"type-guard","validationCode":"if (df.Columns[name] is not UInt16DataFrameColumn)\n    throw new InvalidOperationException($\"Column '{name}' is {df.Columns[name]?.DataType?.Name ?? \"missing\"}, expected UInt16\");","typeGuard":"static bool IsUInt16Column(DataFrameColumn c) => c is UInt16DataFrameColumn;","tryCatchPattern":"try { var col = df.Columns.GetUInt16Column(name); }\ncatch (ArgumentException ex) { /* fall back to conversion path */ }","preventionTips":["Match the getter to the column's actual DataType","Guard with 'is' pattern matching","Convert explicitly rather than casting implicitly","Pin schemas when importing data"],"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"}