dotnet/machinelearning · error · System.ArgumentException

Column lengths are mismatched

Error message

Column lengths are mismatched

What it means

FillNulls(IList<object> values) requires one replacement value per column; when values.Count != Columns.Count it throws ArgumentException with Strings.MismatchedColumnLengths ('Column lengths are mismatched') naming the `values` parameter. The library maps values[i] positionally onto columns[i], so a count mismatch would silently mis-assign values.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrame.cs:484

        {
            DataFrame ret = inPlace ? this : Clone();
            for (int i = 0; i < ret.Columns.Count; i++)
            {
                ret.Columns[i].FillNulls(value, inPlace: true);
            }
            return ret;
        }

        /// <summary>
        /// Fills <see langword="null" /> values in each column with values from <paramref name="values"/>.
        /// </summary>
        /// <param name="values">The values to replace <see langword="null" /> with, one value per column. Should be equal to the number of columns in this <see cref="DataFrame"/>. </param>
        /// <param name="inPlace">A boolean flag to indicate if the operation should be in place</param>
        /// <returns>A new <see cref="DataFrame"/> if <paramref name="inPlace"/> is not set. Returns this <see cref="DataFrame"/> otherwise.</returns>
        public DataFrame FillNulls(IList<object> values, bool inPlace = false)
        {
            if (values.Count != Columns.Count)
                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(values));

            DataFrame ret = inPlace ? this : Clone();
            for (int i = 0; i < ret.Columns.Count; i++)
            {
                Columns[i].FillNulls(values[i], inPlace: true);
            }
            return ret;
        }

        private void ResizeByOneAndAppend(DataFrameColumn column, object value)
        {
            long length = column.Length;
            column.Resize(length + 1);
            column[length] = value;
        }

        /// <summary> 
        /// Appends rows to the DataFrame 

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Build the values list dynamically: df.Columns.Select(c => (object)fillerFor(c)).ToList() so its count always matches.
  2. Assert values.Count == df.Columns.Count before the call and fail with your own clearer message.
  3. Catch ArgumentException to report the expected vs supplied count.
  4. If you only need one scalar for all columns, call df.FillNulls(object) (single-value overload) instead.

Example fix

// before
df.FillNulls(new object[] { 0, "none" }); // 3 columns
// after
var vals = df.Columns.Select(c => c.DataType == typeof(string) ? (object)"none" : 0).ToList();
df.FillNulls(vals);
Defensive patterns

Strategy: validation

Validate before calling

if (values.Count != df.Columns.Count)
    throw new ArgumentException($"FillNulls needs {df.Columns.Count} values, got {values.Count}");
df.FillNulls(values);

Try / catch

try { df.FillNulls(values); }
catch (ArgumentException ex) { logger.LogError(ex, "Filler count mismatch"); throw; }

Prevention

When it happens

Trigger: df.FillNulls(new object[]{0, 0}) on a 3-column DataFrame, or FillNulls with a list built for a different schema version (columns added/removed earlier in the pipeline).

Common situations: Hardcoded filler arrays that drift from the schema; columns appended after the filler list was written; reusing one filler list across DataFrames with different column counts.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/b09a960ff51fc774. Report an issue: GitHub.