Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in set_labels of MultiIndex #19057

Closed
Mofef opened this issue Jan 3, 2018 · 0 comments · Fixed by #19058
Closed

Bug in set_labels of MultiIndex #19057

Mofef opened this issue Jan 3, 2018 · 0 comments · Fixed by #19058
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions MultiIndex
Milestone

Comments

@Mofef
Copy link
Contributor

Mofef commented Jan 3, 2018

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()
Mofef added a commit to Mofef/pandas that referenced this issue Jan 3, 2018
@jreback jreback added Bug Dtype Conversions Unexpected or buggy dtype conversions MultiIndex labels Jan 4, 2018
@jreback jreback added this to the 0.23.0 milestone Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants