{"record":{"id":"818fa18811752d15","repo":"dotnet/machinelearning","slug":"nameof-groupbycolumnindex","errorCode":null,"errorMessage":"nameof(groupByColumnIndex)","messagePattern":"nameof\\(groupByColumnIndex\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/GroupBy.cs","lineNumber":111,"sourceCode":"                return _rows.GetEnumerator();\n            }\n\n            IEnumerator IEnumerable.GetEnumerator()\n            {\n                return _rows.GetEnumerator();\n            }\n        }\n\n        #endregion\n\n        private readonly int _groupByColumnIndex;\n        private readonly IDictionary<TKey, ICollection<long>> _keyToRowIndicesMap;\n        private readonly DataFrame _dataFrame;\n\n        public GroupBy(DataFrame dataFrame, int groupByColumnIndex, IDictionary<TKey, ICollection<long>> keyToRowIndices)\n        {\n            if (dataFrame.Columns.Count < groupByColumnIndex || groupByColumnIndex < 0)\n                throw new ArgumentException(nameof(groupByColumnIndex));\n            _groupByColumnIndex = groupByColumnIndex;\n            _keyToRowIndicesMap = keyToRowIndices ?? throw new ArgumentException(nameof(keyToRowIndices));\n            _dataFrame = dataFrame;\n        }\n\n        private delegate void ColumnDelegate(int columnIndex, long rowIndex, ICollection<long> rows, TKey key, bool firstGroup);\n        private delegate void GroupByColumnDelegate(long rowNumber, TKey key);\n        private void EnumerateColumnsWithRows(GroupByColumnDelegate groupByColumnDelegate, ColumnDelegate columnDelegate, params string[] columnNames)\n        {\n            long rowNumber = 0;\n            bool firstGroup = true;\n            foreach (KeyValuePair<TKey, ICollection<long>> pairs in _keyToRowIndicesMap)\n            {\n                groupByColumnDelegate(rowNumber, pairs.Key);\n                ICollection<long> rows = pairs.Value;\n                IEnumerable<string> columns = columnNames;\n                if (columnNames == null || columnNames.Length == 0)\n                    columns = _dataFrame.GetColumnNames();","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/GroupBy.cs#L93-L129","documentation":"The GroupBy constructor validates that groupByColumnIndex lies within the DataFrame's columns: if dataFrame.Columns.Count < groupByColumnIndex or the index is negative, it throws ArgumentException(nameof(groupByColumnIndex)). Note the check uses '<' not '<=', so an index exactly equal to Columns.Count slips through here and fails later — treat any index >= Count as invalid.","triggerScenarios":"Calling df.GroupBy(keyColumnIndex) with an index >= df.Columns.Count or a negative index, e.g. a hardcoded constant or a stale ordinal after columns were dropped.","commonSituations":"Hardcoded column positions broken by schema changes; computing the index from user input; off-by-one assuming the last ordinal (Count) is valid.","solutions":["Validate before calling: if (idx < 0 || idx >= df.Columns.Count) handle/throw.","Prefer the string-based GroupBy(columnName) overload to avoid ordinal mistakes.","Look up the ordinal via df.Columns.IndexOf(name) right before grouping.","Fix off-by-one: the maximum valid index is df.Columns.Count - 1."],"exampleFix":"// before\ndf.GroupBy(df.Columns.Count); // invalid\n// after\nint idx = df.Columns.IndexOf(\"Key\");\nif (idx < 0 || idx >= df.Columns.Count)\n    throw new ArgumentOutOfRangeException(nameof(idx));\ndf.GroupBy(idx);","handlingStrategy":"validation","validationCode":"if (idx < 0 || idx >= df.Columns.Count)\n    throw new ArgumentOutOfRangeException(nameof(idx), $\"Column index {idx} out of 0..{df.Columns.Count - 1}\");","typeGuard":"bool isValidColumnIndex(DataFrame df, int idx) => idx >= 0 && idx < df.Columns.Count;","tryCatchPattern":"try { var gb = df.GroupBy(idx); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"groupByColumnIndex\"))\n{ /* invalid column ordinal: resolve by name */ }","preventionTips":["Group by column name, not ordinal, when possible.","Resolve ordinals with df.Columns.IndexOf(name) at call time.","Recompute ordinals after any schema change.","Remember the max valid index is Columns.Count - 1."],"tags":["argument-exception","dataframe","groupby","index-out-of-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"}