dotnet/machinelearning · error · ArgumentException
Strings.CannotResizeDown
Error message
Strings.CannotResizeDown
What it means
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.
Source
Thrown at src/Microsoft.Data.Analysis/PrimitiveColumnContainer.cs:109
{
throw new ArgumentException(Strings.InconsistentNullBitMapAndLength, nameof(nullBitMap));
}
nullDataFrameBuffer = new ReadOnlyDataFrameBuffer<byte>(nullBitMap, bitMapBufferLength);
}
NullBitMapBuffers.Add(nullDataFrameBuffer);
Length = length;
NullCount = nullCount;
}
public PrimitiveColumnContainer(long length = 0, T? defaulValue = null)
{
AppendMany(defaulValue, length);
}
public void Resize(long length)
{
if (length < Length)
throw new ArgumentException(Strings.CannotResizeDown, nameof(length));
AppendMany(default, length - Length);
}
public void Append(T? value)
{
if (Buffers.Count == 0)
{
Buffers.Add(new DataFrameBuffer<T>());
NullBitMapBuffers.Add(new DataFrameBuffer<byte>());
}
if (Buffers[Buffers.Count - 1].Length == ReadOnlyDataFrameBuffer<T>.MaxCapacity)
{
Buffers.Add(new DataFrameBuffer<T>());
NullBitMapBuffers.Add(new DataFrameBuffer<byte>());
}
DataFrameBuffer<T> mutableLastBuffer = Buffers.GetOrCreateMutable(Buffers.Count - 1);View on GitHub (pinned to 7b76e69cf9)
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.
Example fix
// before
container.Resize(50); // container.Length is 100 -> throws
// after
if (container.Length > 50)
{
var trimmed = new PrimitiveColumnContainer<T>();
for (long i = 0; i < 50; i++) trimmed.Append(container[i]);
container = trimmed;
}
else
{
container.Resize(50);
} Defensive patterns
Strategy: validation
Validate before calling
if (newLength >= container.Length) { container.Resize(newLength); } Type guard
static bool CanResizeTo<T>(PrimitiveColumnContainer<T> c, long newLength) => newLength >= c.Length;
Try / catch
try { container.Resize(newLength); }
catch (ArgumentException ex) when (ex.ParamName == "length")
{
// build a new container with the first newLength values instead
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- string.Format(Strings.MismatchedValueType, typeof(string))
- String.Format(Strings.MultipleMismatchedValueType, typeof(lo
- Strings.MapIndicesExceedsColumnLength
- Strings.CannotResizeDown
- Strings.InconsistentNullBitMapAndLength
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/4dd19d877198b580.
Report an issue: GitHub.