BUG: avoid unnecessary casting  when unstacking index with unused levels
closes #17845
closes #18562

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