BUG: Fix group index calculation to prevent hitting maximum recursion depth (#21524)
This just replaces tail recursion call with a simple loop. It should have no effect whatsoever on a performance but prevent hitting recursion limits on some input data ( See example in my issue here: https://github.com/pandas-dev/pandas/issues/21524 )

- [x] closes #21524 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry
