{"record":{"id":"9c6f965fb16db664","repo":"dotnet/machinelearning","slug":"parameter-count-exceeds-the-number-of-rows-0-in","errorCode":null,"errorMessage":"Parameter.Count exceeds the number of rows({0}) in the DataFrame ","messagePattern":"Parameter\\.Count exceeds the number of rows\\((.+?)\\) in the DataFrame ","errorType":"validation","errorClass":"System.ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrame.cs","lineNumber":339,"sourceCode":"            DataFrame df = inPlace ? this : Clone();\n            for (int i = 0; i < df.Columns.Count; i++)\n            {\n                DataFrameColumn column = df.Columns[i];\n                column.SetName(column.Name + suffix);\n                df.OnColumnsChanged();\n            }\n            return df;\n        }\n\n        /// <summary>\n        /// Returns a random sample of rows\n        /// </summary>\n        /// <param name=\"numberOfRows\">Number of rows in the returned DataFrame</param>\n        public DataFrame Sample(int numberOfRows)\n        {\n            if (numberOfRows > Rows.Count)\n            {\n                throw new ArgumentException(string.Format(Strings.ExceedsNumberOfRows, Rows.Count), nameof(numberOfRows));\n            }\n\n            int shuffleLowerLimit = 0;\n            int shuffleUpperLimit = (int)Math.Min(Int32.MaxValue, Rows.Count);\n\n            int[] shuffleArray = Enumerable.Range(0, shuffleUpperLimit).ToArray();\n            Random rand = new Random();\n            while (shuffleLowerLimit < numberOfRows)\n            {\n                int randomIndex = rand.Next(shuffleLowerLimit, shuffleUpperLimit);\n                int temp = shuffleArray[shuffleLowerLimit];\n                shuffleArray[shuffleLowerLimit] = shuffleArray[randomIndex];\n                shuffleArray[randomIndex] = temp;\n                shuffleLowerLimit++;\n            }\n            ArraySegment<int> segment = new ArraySegment<int>(shuffleArray, 0, shuffleLowerLimit);\n\n            PrimitiveDataFrameColumn<int> indices = new PrimitiveDataFrameColumn<int>(\"indices\", segment);","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrame.cs#L321-L357","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Clamp the request: df.Sample((int)Math.Min(numberOfRows, df.Rows.Count)).","Check df.Rows.Count first and skip or log when the DataFrame is smaller than the requested sample.","If sampling WITH replacement is intended, implement it manually with random indices instead of Sample.","Catch ArgumentException and fall back to sampling the full DataFrame (Rows.Count)."],"exampleFix":"// before\nvar sample = df.Sample(1000);\n// after\nint n = (int)Math.Min(1000, df.Rows.Count);\nvar sample = n > 0 ? df.Sample(n) : df;","handlingStrategy":"validation","validationCode":"int n = (int)Math.Min(numberOfRows, df.Rows.Count);\nif (n <= 0) throw new InvalidOperationException(\"Nothing to sample\");\nvar sample = df.Sample(n);","typeGuard":null,"tryCatchPattern":"try { sample = df.Sample(numberOfRows); }\ncatch (ArgumentException ex) { logger.LogWarning(ex, \"Requested sample too large\"); sample = df; }","preventionTips":["Always clamp sample size against df.Rows.Count","Re-check row count after any filter operation before sampling","Avoid hard-coded sample sizes; derive them from the data"],"tags":["argument","sampling","row-count","range"],"backgroundTag":"argument-out-of-range","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"}