{"record":{"id":"6c01dcae0fe135a3","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(T))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(T\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":223,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"PrimitiveDataFrameColumn{T}\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"PrimitiveDataFrameColumn{T}\"/>.</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 PrimitiveDataFrameColumn<T> GetPrimitiveColumn<T>(string name)\n            where T : unmanaged\n        {\n            DataFrameColumn column = this[name];\n            if (column is PrimitiveDataFrameColumn<T> ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(T)), nameof(T));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"DateTimeDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"DateTimeDataFrameColumn\"/>.</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 DateTimeDataFrameColumn GetDateTimeColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is DateTimeDataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(DateTime)));\n        }","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L205-L241","documentation":"GetPrimitiveColumn<T> fetches the column by name and throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(T)) if it is not a PrimitiveDataFrameColumn<T>. This guards against invalid type-based column casts.","triggerScenarios":"df.Columns.GetPrimitiveColumn<int>(\"name\") where the stored column is not PrimitiveDataFrameColumn<int> (wrong primitive type, or a string/datetime/arrow column).","commonSituations":"CSV inference giving float where int was expected, schema drift across files, assuming int columns that contain nulls are Int32 instead of Nullable Int64 defaults.","solutions":["Check column.DataType first and request the matching T (e.g. use GetPrimitiveColumn<long> for Int64).","Inspect df.Columns[name].DataType (or df.Schema) and branch on the actual type.","Convert the column (Clone/Cast) to the desired primitive type before retrieval."],"exampleFix":"// before\nvar col = df.Columns.GetPrimitiveColumn<int>(\\\"Age\\\"); // Age is Int64\n// after\nif (df.Columns[\"Age\"].DataType == typeof(long))\n    var col = df.Columns.GetPrimitiveColumn<long>(\"Age\");","handlingStrategy":"type-guard","validationCode":"var type = df.Columns[\"Age\"].DataType;\nvar col = type == typeof(int)\n    ? df.Columns.GetPrimitiveColumn<int>(\"Age\")\n    : throw new InvalidOperationException($\"'Age' is {type}, expected int\");","typeGuard":"bool IsPrimitive<T>(DataFrameColumn c) => c is PrimitiveDataFrameColumn<T>;","tryCatchPattern":"try\n{\n    var col = df.Columns.GetPrimitiveColumn<int>(name);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"T\")\n{\n    // inspect column.DataType and retry with matching T\n}","preventionTips":["Inspect column.DataType (or df.Schema) before typed getters.","Account for loader type inference (int columns often arrive as long/double).","Prefer pattern matching (is PrimitiveDataFrameColumn<T>) over blind casts."],"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"}