|  | 
| 7 | 7 | from pandas import compat | 
| 8 | 8 | import numpy as np | 
| 9 | 9 | 
 | 
|  | 10 | +from sys import getsizeof | 
| 10 | 11 | import pandas.tslib as tslib | 
| 11 | 12 | import pandas.lib as lib | 
| 12 | 13 | import pandas.algos as _algos | 
|  | 
| 17 | 18 | from pandas.core.common import isnull, array_equivalent | 
| 18 | 19 | import pandas.core.common as com | 
| 19 | 20 | from pandas.core.common import (_values_from_object, is_float, is_integer, | 
| 20 |  | - ABCSeries, _ensure_object) | 
|  | 21 | + ABCSeries, _ensure_object, _ensure_int64) | 
| 21 | 22 | from pandas.core.config import get_option | 
| 22 | 23 | 
 | 
| 23 | 24 | # simplify | 
| @@ -2680,13 +2681,13 @@ def _set_labels(self, labels, level=None, copy=False, validate=True, | 
| 2680 | 2681 |  raise ValueError('Length of labels must match length of levels.') | 
| 2681 | 2682 | 
 | 
| 2682 | 2683 |  if level is None: | 
| 2683 |  | - new_labels = FrozenList(_ensure_frozen(v, copy=copy)._shallow_copy() | 
| 2684 |  | - for v in labels) | 
|  | 2684 | + new_labels = FrozenList(_ensure_frozen(lab, lev, copy=copy)._shallow_copy() | 
|  | 2685 | + for lev, lab in zip(self.levels, labels)) | 
| 2685 | 2686 |  else: | 
| 2686 | 2687 |  level = [self._get_level_number(l) for l in level] | 
| 2687 | 2688 |  new_labels = list(self._labels) | 
| 2688 |  | - for l, v in zip(level, labels): | 
| 2689 |  | - new_labels[l] = _ensure_frozen(v, copy=copy)._shallow_copy() | 
|  | 2689 | + for l, lev, lab in zip(level, self.levels, labels): | 
|  | 2690 | + new_labels[l] = _ensure_frozen(lab, lev, copy=copy)._shallow_copy() | 
| 2690 | 2691 |  new_labels = FrozenList(new_labels) | 
| 2691 | 2692 | 
 | 
| 2692 | 2693 |  self._labels = new_labels | 
| @@ -2824,6 +2825,14 @@ def _array_values(self): | 
| 2824 | 2825 |  def dtype(self): | 
| 2825 | 2826 |  return np.dtype('O') | 
| 2826 | 2827 | 
 | 
|  | 2828 | + @cache_readonly | 
|  | 2829 | + def nbytes(self): | 
|  | 2830 | + """ return the number of bytes in the underlying data """ | 
|  | 2831 | + level_nbytes = sum(( i.nbytes for i in self.levels )) | 
|  | 2832 | + label_nbytes = sum(( i.nbytes for i in self.labels )) | 
|  | 2833 | + names_nbytes = sum(( getsizeof(i) for i in self.names )) | 
|  | 2834 | + return level_nbytes + label_nbytes + names_nbytes | 
|  | 2835 | + | 
| 2827 | 2836 |  def __repr__(self): | 
| 2828 | 2837 |  encoding = get_option('display.encoding') | 
| 2829 | 2838 |  attrs = [('levels', default_pprint(self.levels)), | 
| @@ -4361,7 +4370,7 @@ def insert(self, loc, item): | 
| 4361 | 4370 |  lev_loc = level.get_loc(k) | 
| 4362 | 4371 | 
 | 
| 4363 | 4372 |  new_levels.append(level) | 
| 4364 |  | - new_labels.append(np.insert(labels, loc, lev_loc)) | 
|  | 4373 | + new_labels.append(np.insert(_ensure_int64(labels), loc, lev_loc)) | 
| 4365 | 4374 | 
 | 
| 4366 | 4375 |  return MultiIndex(levels=new_levels, labels=new_labels, | 
| 4367 | 4376 |  names=self.names, verify_integrity=False) | 
| @@ -4474,8 +4483,8 @@ def _ensure_index(index_like, copy=False): | 
| 4474 | 4483 |  return Index(index_like) | 
| 4475 | 4484 | 
 | 
| 4476 | 4485 | 
 | 
| 4477 |  | -def _ensure_frozen(array_like, copy=False): | 
| 4478 |  | - array_like = np.asanyarray(array_like, dtype=np.int_) | 
|  | 4486 | +def _ensure_frozen(array_like, categories, copy=False): | 
|  | 4487 | + array_like = com._coerce_indexer_dtype(array_like, categories) | 
| 4479 | 4488 |  array_like = array_like.view(FrozenNDArray) | 
| 4480 | 4489 |  if copy: | 
| 4481 | 4490 |  array_like = array_like.copy() | 
|  | 
0 commit comments