{"record":{"id":"9b23b89545b536a0","repo":"dotnet/machinelearning","slug":"strings-badcolumncast-formatted-with-column-datat-9b23b8","errorCode":null,"errorMessage":"Strings.BadColumnCast (formatted with column.DataType, typeof(DateTime))","messagePattern":"Strings\\.BadColumnCast \\(formatted with column\\.DataType, typeof\\(DateTime\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":240,"sourceCode":"\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        }\n\n        /// <summary>\n        /// Gets the <see cref=\"ArrowStringDataFrameColumn\"/> with the specified <paramref name=\"name\"/>.\n        /// </summary>\n        /// <param name=\"name\">The name of the column</param>\n        /// <returns><see cref=\"ArrowStringDataFrameColumn\"/>.</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 ArrowStringDataFrameColumn GetArrowStringColumn(string name)\n        {\n            DataFrameColumn column = this[name];\n            if (column is ArrowStringDataFrameColumn ret)\n            {\n                return ret;\n            }\n\n            throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));\n        }","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L222-L258","documentation":"GetDateTimeColumn returns the named column as a DateTimeDataFrameColumn or throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(DateTime)) when the column's type differs. Note this throw omits a paramName, unlike GetPrimitiveColumn.","triggerScenarios":"df.Columns.GetDateTimeColumn(\"ts\") where the column is e.g. a string or PrimitiveDataFrameColumn, not DateTimeDataFrameColumn.","commonSituations":"Date columns parsed as strings from CSV, timestamp columns stored as long (unix epoch), timezone columns of differing storage types.","solutions":["Verify column.DataType == typeof(DateTime) before calling, or catch and convert.","Parse the underlying column into a new DateTimeDataFrameColumn first, add it, then fetch it.","Inspect the actual column kind via pattern matching (is DateTimeDataFrameColumn)."],"exampleFix":"// before\nvar ts = df.Columns.GetDateTimeColumn(\"ts\"); // ts is string column\n// after\nvar tsStr = df.Columns.GetArrowStringColumn(\"ts\");\nvar ts = new DateTimeDataFrameColumn(\"ts\", tsStr.Select(DateTime.Parse));\ndf.Columns.Add(ts);","handlingStrategy":"type-guard","validationCode":"if (!(df.Columns[\"ts\"] is DateTimeDataFrameColumn))\n    throw new InvalidOperationException($\"'ts' is {df.Columns[\"ts\"].DataType}, not DateTime\");\nvar ts = df.Columns.GetDateTimeColumn(\"ts\");","typeGuard":"bool IsDateTimeColumn(DataFrameColumn c) => c is DateTimeDataFrameColumn;","tryCatchPattern":"try\n{\n    var ts = df.Columns.GetDateTimeColumn(name);\n}\ncatch (ArgumentException)\n{\n    // convert/parse the actual column type into DateTimeDataFrameColumn\n}","preventionTips":["Confirm date columns are stored as DateTimeDataFrameColumn after ingestion, not string/long.","Normalize timestamp columns at load time.","Pattern-match column kind before typed getters."],"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"}