{"record":{"id":"4dd19d877198b580","repo":"dotnet/machinelearning","slug":"strings-cannotresizedown-4dd19d","errorCode":null,"errorMessage":"Strings.CannotResizeDown","messagePattern":"Strings\\.CannotResizeDown","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/PrimitiveColumnContainer.cs","lineNumber":109,"sourceCode":"                {\n                    throw new ArgumentException(Strings.InconsistentNullBitMapAndLength, nameof(nullBitMap));\n                }\n                nullDataFrameBuffer = new ReadOnlyDataFrameBuffer<byte>(nullBitMap, bitMapBufferLength);\n            }\n            NullBitMapBuffers.Add(nullDataFrameBuffer);\n            Length = length;\n            NullCount = nullCount;\n        }\n\n        public PrimitiveColumnContainer(long length = 0, T? defaulValue = null)\n        {\n            AppendMany(defaulValue, length);\n        }\n\n        public void Resize(long length)\n        {\n            if (length < Length)\n                throw new ArgumentException(Strings.CannotResizeDown, nameof(length));\n            AppendMany(default, length - Length);\n        }\n\n        public void Append(T? value)\n        {\n            if (Buffers.Count == 0)\n            {\n                Buffers.Add(new DataFrameBuffer<T>());\n                NullBitMapBuffers.Add(new DataFrameBuffer<byte>());\n            }\n\n            if (Buffers[Buffers.Count - 1].Length == ReadOnlyDataFrameBuffer<T>.MaxCapacity)\n            {\n                Buffers.Add(new DataFrameBuffer<T>());\n                NullBitMapBuffers.Add(new DataFrameBuffer<byte>());\n            }\n\n            DataFrameBuffer<T> mutableLastBuffer = Buffers.GetOrCreateMutable(Buffers.Count - 1);","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/PrimitiveColumnContainer.cs#L91-L127","documentation":"PrimitiveColumnContainer.Resize only grows the container: it appends `default` values up to the requested length. If the requested length is smaller than the current Length, the existing data would have to be discarded, which the API refuses to do, so it throws ArgumentException with paramName `length`. Shrink by creating a new container instead.","triggerScenarios":"Calling Resize(newLength) on a PrimitiveColumnContainer<T> (directly or through PrimitiveDataFrameColumn.Resize) where newLength < container.Length, e.g. resizing a column from 100 rows to 50.","commonSituations":"Truncating a DataFrame after a filter computed a smaller row count, reusing a column object across datasets of decreasing size, or code that assumes Resize works like List<T>.RemoveRange.","solutions":["Only call Resize with a length greater than or equal to the current Length (check container.Length first).","To shrink, build a new container/column and copy the first N values with Append, then replace the reference.","Use DataFrame/Column APIs that produce a new object (e.g. slicing) instead of mutating the existing container."],"exampleFix":"// before\ncontainer.Resize(50); // container.Length is 100 -> throws\n// after\nif (container.Length > 50)\n{\n    var trimmed = new PrimitiveColumnContainer<T>();\n    for (long i = 0; i < 50; i++) trimmed.Append(container[i]);\n    container = trimmed;\n}\nelse\n{\n    container.Resize(50);\n}","handlingStrategy":"validation","validationCode":"if (newLength >= container.Length) { container.Resize(newLength); }","typeGuard":"static bool CanResizeTo<T>(PrimitiveColumnContainer<T> c, long newLength) => newLength >= c.Length;","tryCatchPattern":"try { container.Resize(newLength); }\ncatch (ArgumentException ex) when (ex.ParamName == \"length\")\n{\n    // build a new container with the first newLength values instead\n}","preventionTips":["Treat Resize as grow-only; document this at call sites","Implement shrinking through new-container copy helpers","Never reuse long-lived column objects across datasets of different sizes"],"tags":["argumentexception","resize","dataframe","immutable-length"],"backgroundTag":"invalid-argument-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}