REF: `refactor_levels` for `Styler.format_index`
Have moved some of the changes from #43101. The PR was split to try and debug the consistently failing CI which can't reproduce locally, and is unrelated to the code changes.

Please merge this one, it moves a private method to the base class script.