BUG: incorrect set_labels in MI
State: closed

This fixes #19057


Labels:
name: Bug
name: Dtype Conversions
description: Unexpected or buggy dtype conversions
name: MultiIndex

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=h1) Report
> Merging [#19058](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/821028f68fa75c0d6d341148a3ab6f08bc8d3515?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/19058/graphs/tree.svg?height=150&width=650&token=eZ4WkYLtcO&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #19058      +/-   ##
==========================================
+ Coverage   91.53%   91.53%   +<.01%     
==========================================
  Files         148      148              
  Lines       48688    48689       +1     
==========================================
+ Hits        44566    44567       +1     
  Misses       4122     4122
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.9% <100%> (ø)` | :arrow_up: |
| #single | `41.62% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/multi.py](https://codecov.io/gh/pandas-dev/pandas/pull/19058/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9tdWx0aS5weQ==) | `96.22% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=footer). Last update [821028f...48fb843](https://codecov.io/gh/pandas-dev/pandas/pull/19058?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

cc @toobaz 

any thoughts here
Looks good to me. As for tests, I guess it's just a matter of understanding what went wrong in [this one](https://github.com/pandas-dev/pandas/blob/master/pandas/tests/indexes/test_multi.py#L213), and fix it.
you probably mean https://github.com/pandas-dev/pandas/blob/93033151a8d8aaa650a81df9f41347758bf6c393/pandas/tests/indexes/test_multi.py#L287 set_levels works correctly iirc.
The issue with the test is that self.index has the same magnitude of categories in each level. (i.e. the length is each smaller than int8_max).  So the misalignment does not have an effect. Also there is no misalignment if the labels get replaced in the first `n` levels. 
I would like to take care of the tests later. 
  
> you probably mean

yep, sorry, actually this assertion:
https://github.com/pandas-dev/pandas/blob/93033151a8d8aaa650a81df9f41347758bf6c393/pandas/tests/indexes/test_multi.py#L302

But I agree with your explanation.
@Mofef tests need to be in the same issue as the PR fixing.
@jreback Sorry I don't understand, you mean i should also add tests to this PR?

In the meanwhile i looked into test_multi.py. My initial idea was to just increase the size of the minor level with something like 
```python
    def setup_method(self, method):
        major_axis = Index(['foo', 'bar', 'baz', 'qux'])
        minor_axis = Index(range(2, 128))

        major_labels = np.random.choice(range(4), 128)
        minor_labels = np.array(range(128))
...
```
However, that doesn't seem to be feasible since a lot of test methods rely on the current "reference index". So I get ~50 presumably false negative tests. 
Another option would be to create a bigger `MultiIndex` down in `test_set_labels`. On the other hand it's probably a good idea to check each operation against such an index with `levels` of different `dtype`.
What do you think?
  
@Mofef yes you need a test, you can use the one that you put in the issue. you need to assert that this works (eg. construct the expected MI).
Done. I had to define another test method, with its own `assert_matching` function, since inside `test_set_labels` the expected labels get casted to int8. Imho this should happen outside of a function called "assert_matching"

I also stumbled upon #19092
 
Edit: I forgot about the whatsnew...
  
Cool, thanks. PTAL
looks good. ping on green.
@jreback https://travis-ci.org/pandas-dev/pandas/jobs/325457802#L448
```
648.46s$ git fetch origin +refs/pull/19058/merge:
fatal: unable to access 'https://github.com/pandas-dev/pandas.git/': Failed to connect to github.com port 443: Connection timed out
```
Sorry about the extra commits @Mofef. I fixed a merge conflict in the release notes and the CI services apparently had an issue with that. They're running again now.
Oh ok no problem. thank you :) 
thanks @Mofef