{"record":{"id":"eebf7e734f783d86","repo":"dotnet/machinelearning","slug":"string-format-strings-mismatchedvaluetype-datatyp","errorCode":null,"errorMessage":"string.Format(Strings.MismatchedValueType, DataType)","messagePattern":"string\\.Format\\(Strings\\.MismatchedValueType, DataType\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs","lineNumber":245,"sourceCode":"\n        protected virtual PrimitiveDataFrameColumn<T> CreateNewColumn(string name, long length = 0)\n        {\n            return new PrimitiveDataFrameColumn<T>(name, length);\n        }\n\n        protected T? GetTypedValue(long rowIndex) => _columnContainer[rowIndex];\n\n        protected override object GetValue(long rowIndex) => GetTypedValue(rowIndex);\n\n        protected override void SetValue(long rowIndex, object value)\n        {\n            if (value == null || value.GetType() == typeof(T))\n            {\n                _columnContainer[rowIndex] = (T?)value;\n            }\n            else\n            {\n                throw new ArgumentException(string.Format(Strings.MismatchedValueType, DataType), nameof(value));\n            }\n        }\n\n        public new T? this[long rowIndex]\n        {\n            get => GetTypedValue(rowIndex);\n            set => _columnContainer[rowIndex] = value;\n        }\n\n        /// <inheritdoc/>\n        public override double Median()\n        {\n            // Not the most efficient implementation. Using a selection algorithm here would be O(n) instead of O(nLogn)\n            var notNullValuesCount = Length - NullCount;\n            if (notNullValuesCount == 0)\n                return 0;\n\n            PrimitiveDataFrameColumn<long> sortIndices = GetSortIndices();","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs#L227-L263","documentation":"SetValue requires the boxed value to be exactly of type T (or null) since the column storage is T?; otherwise it throws ArgumentException naming the value parameter. DataFrameColumns are strongly typed, so a value of a different CLR type cannot be stored without conversion.","triggerScenarios":"Calling SetValue(rowIndex, value) with a boxed value whose GetType() != typeof(T), e.g. storing int into a PrimitiveDataFrameColumn<long> or double into an int column.","commonSituations":"Populating columns from untyped data (JSON, CSV, database rows) where numerics arrive as different widths, or assigning a nullable/boxed value directly.","solutions":["Convert the value to the column's element type T before SetValue, e.g. (long)someInt","Check column.DataType and dispatch conversion accordingly","Use the strongly typed indexer on PrimitiveDataFrameColumn<T> instead of the base-class API"],"exampleFix":"// before\ncolumn.SetValue(row, 42); // column is PrimitiveDataFrameColumn<long>\n// after\ncolumn.SetValue(row, (long)42);","handlingStrategy":"type-guard","validationCode":"if (value != null && value.GetType() != column.DataType) throw new ArgumentException($\"Expected {column.DataType}, got {value.GetType()}\");","typeGuard":"bool isCompatible(object v) => v == null || v.GetType() == typeof(T);","tryCatchPattern":"try { column.SetValue(row, value); } catch (ArgumentException) { /* convert value to T and retry */ }","preventionTips":["Check DataType before storing boxed values","Convert incoming untyped data to the column's element type","Use the strongly typed indexer on PrimitiveDataFrameColumn<T>"],"tags":["type-mismatch","argument","dataframe"],"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"}