|
14 | 14 | import pandas.core.common as com
|
15 | 15 | from pandas.util.decorators import cache_readonly
|
16 | 16 |
|
17 |
| -from pandas.core.common import (CategoricalDtype, ABCSeries, isnull, notnull, |
| 17 | +from pandas.core.common import (CategoricalDtype, ABCSeries, ABCCategoricalIndex, isnull, notnull, |
18 | 18 | is_categorical_dtype, is_integer_dtype, is_object_dtype,
|
19 | 19 | _possibly_infer_to_datetimelike, get_dtype_kinds,
|
20 | 20 | is_list_like, is_sequence, is_null_slice, is_bool,
|
| 21 | + is_dtypes_equal, |
21 | 22 | _ensure_platform_int, _ensure_object, _ensure_int64,
|
22 | 23 | _coerce_indexer_dtype, _values_from_object, take_1d)
|
23 | 24 | from pandas.util.terminal import get_terminal_size
|
@@ -79,7 +80,7 @@ def f(self, other):
|
79 | 80 |
|
80 | 81 | def maybe_to_categorical(array):
|
81 | 82 | """ coerce to a categorical if a series is given """
|
82 |
| - if isinstance(array, ABCSeries): |
| 83 | + if isinstance(array, (ABCSeries, ABCCategoricalIndex)): |
83 | 84 | return array.values
|
84 | 85 | return array
|
85 | 86 |
|
@@ -233,12 +234,17 @@ def __init__(self, values, categories=None, ordered=False, name=None, fastpath=F
|
233 | 234 | cat = values
|
234 | 235 | if isinstance(values, ABCSeries):
|
235 | 236 | cat = values.values
|
| 237 | + if isinstance(values, ABCCategoricalIndex): |
| 238 | + ordered = values.ordered |
| 239 | + cat = values.values |
| 240 | + |
236 | 241 | if categories is None:
|
237 | 242 | categories = cat.categories
|
238 | 243 | values = values.__array__()
|
239 | 244 |
|
240 | 245 | elif isinstance(values, Index):
|
241 |
| - pass |
| 246 | + #values = np.array(values) |
| 247 | + ordered = True |
242 | 248 |
|
243 | 249 | else:
|
244 | 250 |
|
@@ -302,11 +308,27 @@ def copy(self):
|
302 | 308 | return Categorical(values=self._codes.copy(),categories=self.categories,
|
303 | 309 | name=self.name, ordered=self.ordered, fastpath=True)
|
304 | 310 |
|
| 311 | + def astype(self, dtype): |
| 312 | + """ coerce this type to another dtype """ |
| 313 | + if is_categorical_dtype(dtype): |
| 314 | + return self |
| 315 | + return np.array(self, dtype=dtype) |
| 316 | + |
305 | 317 | @cache_readonly
|
306 | 318 | def ndim(self):
|
307 | 319 | """Number of dimensions of the Categorical """
|
308 | 320 | return self._codes.ndim
|
309 | 321 |
|
| 322 | + @cache_readonly |
| 323 | + def size(self): |
| 324 | + """ return the len of myself """ |
| 325 | + return len(self) |
| 326 | + |
| 327 | + @cache_readonly |
| 328 | + def itemsize(self): |
| 329 | + """ return the size of a single category """ |
| 330 | + return self.categories.itemsize |
| 331 | + |
310 | 332 | def reshape(self, new_shape, **kwargs):
|
311 | 333 | """ compat with .reshape """
|
312 | 334 | return self
|
@@ -1596,14 +1618,20 @@ def _delegate_method(self, name, *args, **kwargs):
|
1596 | 1618 | ##### utility routines #####
|
1597 | 1619 |
|
1598 | 1620 | def _get_codes_for_values(values, categories):
|
1599 |
| - """" |
| 1621 | + """ |
1600 | 1622 | utility routine to turn values into codes given the specified categories
|
1601 | 1623 | """
|
1602 | 1624 |
|
1603 | 1625 | from pandas.core.algorithms import _get_data_algo, _hashtables
|
1604 |
| - if values.dtype != categories.dtype: |
| 1626 | + if not is_dtypes_equal(values.dtype,categories.dtype): |
| 1627 | + values = _ensure_object(values) |
| 1628 | + categories = _ensure_object(categories) |
| 1629 | + |
| 1630 | + if is_object_dtype(values): |
1605 | 1631 | values = _ensure_object(values)
|
| 1632 | + if is_object_dtype(categories): |
1606 | 1633 | categories = _ensure_object(categories)
|
| 1634 | + |
1607 | 1635 | (hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
|
1608 | 1636 | t = hash_klass(len(categories))
|
1609 | 1637 | t.map_locations(_values_from_object(categories))
|
|
0 commit comments