Bug in set_labels of MultiIndex (original) (raw)

import pandas as pd

idx = pd.MultiIndex.from_tuples([(0, i) for i in range(130)]) print idx.labels[1].dtype

--> int16

idx.set_labels(labels=idx.labels[1], level=1, inplace=True) print idx.labels[1].dtype

--> int8

Imho this is caused by the following line, because the erratic assumption that the indices of labels and self.levels would be index-aligned.
https://github.com/pandas-dev/pandas/blame/master/pandas/core/indexes/multi.py#L331
Hence the call to coerce_indexer_dtype casts the given labels to int8. This is because its l'ooking at the number of categories in level 0.

I would suggest to replace

        for l, lev, lab in zip(level, self.levels, labels):
            new_labels[l] = _ensure_frozen(
                lab, lev, copy=copy)._shallow_copy()

with

        for l, lab in zip(level, labels):
            lev = self.levels[l]
            new_labels[l] = _ensure_frozen(
                lab, lev, copy=copy)._shallow_copy()