dotnet/machinelearning · error · System.ArgumentException

Parameter.Count exceeds the number of rows({0}) in the DataF

Error message

Parameter.Count exceeds the number of rows({0}) in the DataFrame 

What it means

DataFrame.Sample(numberOfRows) throws ArgumentException (wrapping Strings.ExceedsNumberOfRows) when the requested sample size exceeds the DataFrame's current row count. Sampling without replacement cannot return more rows than exist, so the library rejects the request up front. The formatted message includes the actual row count.

Source

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

            DataFrame df = inPlace ? this : Clone();
            for (int i = 0; i < df.Columns.Count; i++)
            {
                DataFrameColumn column = df.Columns[i];
                column.SetName(column.Name + suffix);
                df.OnColumnsChanged();
            }
            return df;
        }

        /// <summary>
        /// Returns a random sample of rows
        /// </summary>
        /// <param name="numberOfRows">Number of rows in the returned DataFrame</param>
        public DataFrame Sample(int numberOfRows)
        {
            if (numberOfRows > Rows.Count)
            {
                throw new ArgumentException(string.Format(Strings.ExceedsNumberOfRows, Rows.Count), nameof(numberOfRows));
            }

            int shuffleLowerLimit = 0;
            int shuffleUpperLimit = (int)Math.Min(Int32.MaxValue, Rows.Count);

            int[] shuffleArray = Enumerable.Range(0, shuffleUpperLimit).ToArray();
            Random rand = new Random();
            while (shuffleLowerLimit < numberOfRows)
            {
                int randomIndex = rand.Next(shuffleLowerLimit, shuffleUpperLimit);
                int temp = shuffleArray[shuffleLowerLimit];
                shuffleArray[shuffleLowerLimit] = shuffleArray[randomIndex];
                shuffleArray[randomIndex] = temp;
                shuffleLowerLimit++;
            }
            ArraySegment<int> segment = new ArraySegment<int>(shuffleArray, 0, shuffleLowerLimit);

            PrimitiveDataFrameColumn<int> indices = new PrimitiveDataFrameColumn<int>("indices", segment);

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Clamp the request: df.Sample((int)Math.Min(numberOfRows, df.Rows.Count)).
  2. Check df.Rows.Count first and skip or log when the DataFrame is smaller than the requested sample.
  3. If sampling WITH replacement is intended, implement it manually with random indices instead of Sample.
  4. Catch ArgumentException and fall back to sampling the full DataFrame (Rows.Count).

Example fix

// before
var sample = df.Sample(1000);
// after
int n = (int)Math.Min(1000, df.Rows.Count);
var sample = n > 0 ? df.Sample(n) : df;
Defensive patterns

Strategy: validation

Validate before calling

int n = (int)Math.Min(numberOfRows, df.Rows.Count);
if (n <= 0) throw new InvalidOperationException("Nothing to sample");
var sample = df.Sample(n);

Try / catch

try { sample = df.Sample(numberOfRows); }
catch (ArgumentException ex) { logger.LogWarning(ex, "Requested sample too large"); sample = df; }

Prevention

When it happens

Trigger: df.Sample(n) where n > df.Rows.Count — e.g. calling Sample(100) on a 50-row DataFrame, or using a hard-coded size on a DataFrame filtered down before sampling.

Common situations: A filtering step upstream removed more rows than expected; hardcoded sample sizes from notebook code reused in production; a config value for 'sample size' larger than the dataset.

Related errors


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