BUG: pandas.DataFrame().stack() raise an error, while expected is empty
State: closed

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


Labels:
name: Bug
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode
name: MultiIndex

Comments:
Hello @steveya! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:









There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-11-26 10:50:16 UTC
@steveya is this the correct issue reference?
@jrreback, It should fix the error seen in #36113, this is my first contribution so please let me know the specifics if I have made even an obvious error. Thanks.
@jreback I have made the suggested changes. 
@jreback @TomAugspurger I have updated the code to raise proper error when a Series with a single level index will raise an exception when it is unstacked. a DataFrame with single level of index and column will no longer raise an exception when unstack is called (it will return a Series(). I will dig deeper into @TomAugspurger's example further.
Could we please create a patch for 0.25.3 when releasing ?
@tsu-shiuan thus won't even be backport to 1.x let alone 0.25.x 
@jreback Okay! ☹️ 
@jreback error message added. Thanks.
@jreback should I do anything else to finish this PL? Thank you.
lgtm ping when green-ish
@jreback seems some checks were not successful, I wanted to help but I don't know which tests failed. 
merge master and push again as the CI should be good now.
can you merge master and ping on green
@jreback Sorry I have concluded that I am not sure how to do that. on my end, I have tried this

% git pull origin GH36113
From https://github.com/steveya/pandas
 * branch                GH36113    -> FETCH_HEAD
Already up to date.
% git merge master
Already up to date.
% git push origin GH36113
Everything up-to-date

This was what I did the 12 days ago and nothing seemed to have happened. Can you provide more guides please, thank you.
> @jreback Sorry I have concluded that I am not sure how to do that. on my end, I have tried this
> 
> % git pull origin GH36113
> From https://github.com/steveya/pandas
> 
> * branch                GH36113    -> FETCH_HEAD
>   Already up to date.
>   % git merge master
>   Already up to date.
>   % git push origin GH36113
>   Everything up-to-date
> 
> This was what I did the 12 days ago and nothing seemed to have happened. Can you provide more guides please, thank you.

do 
```
git fetch upstream
git merge upstream/master
git push origin GH36113
```
You may have to resolve conflicts
@jreback I checked out the tests that failed. I cannot reproduce these errors locally and I am not sure how to fix them. Any suggestions?
I see that the failures on py37-32bit persist.
Is it possible that dtype changes when performing stack/unstack?

<details>

=================================== FAILURES ===================================
__________________ test_stack_unstack_empty_frame[None-True] ___________________

dropna = True, fill_value = None

    @pytest.mark.parametrize("dropna", [True, False])
    @pytest.mark.parametrize("fill_value", [None, 0])
    def test_stack_unstack_empty_frame(dropna, fill_value):
        # GH 36113
        result = (
>           DataFrame(dtype=np.intp).stack(dropna=dropna).unstack(fill_value=fill_value)
        )

pandas/tests/frame/test_stack_unstack.py:1191: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
pandas/core/series.py:3872: in unstack
    return unstack(self, level, fill_value)
pandas/core/reshape/reshape.py:431: in unstack
    obj.index, level=level, constructor=obj._constructor_expanddim
pandas/core/reshape/reshape.py:118: in __init__
    self._make_selectors()
pandas/core/reshape/reshape.py:152: in _make_selectors
    remaining_labels = self.sorted_labels[:-1]
pandas/_libs/properties.pyx:33: in pandas._libs.properties.CachedProperty.__get__
    val = self.func(obj)
pandas/core/reshape/reshape.py:139: in sorted_labels
    indexer, to_sort = self._indexer_and_to_sort
pandas/_libs/properties.pyx:33: in pandas._libs.properties.CachedProperty.__get__
    val = self.func(obj)
pandas/core/reshape/reshape.py:132: in _indexer_and_to_sort
    indexer = libalgos.groupsort_indexer(comp_index, ngroups)[0]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   def groupsort_indexer(const int64_t[:] index, Py_ssize_t ngroups):
E   ValueError: Buffer dtype mismatch, expected 'const int64_t' but got 'int'

pandas/_libs/algos.pyx:177: ValueError

</details>

@jreback the error persists after the change on 32bit

ValueError: Buffer dtype mismatch, expected 'const int64_t' but got 'int'
ok try this: https://github.com/pandas-dev/pandas/blob/bc537b77b4d83b4d812785bada333b575fea1bb4/pandas/core/sorting.py#L613

here you want to add
```return ensure_int64(comp_ids), ensure_int64(obs_group_ids)```

if you can also update the Returns sections to indicate these are int64 indexers
@jreback by the Returns section you mean the doc for "compress_group_index" in sorting.py? 
actually this should pass, @steveya can you merge master one more time.
@jreback yay there is only one remaining error in test_pivot.py in 32bit build. 
> _ TestPivot.test_pivot_empty __________________________
[XPASS(strict)] GH 36579: fail on 32-bit system


oh this is easy, you fixed the test, so remove the xfail.
thanks @steveya very nice. thanks for sticking with it!
@jreback @arw2019 @ivanovmg , thank you all for helping me with my first PR.