|
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,
|
@@ -79,7 +79,7 @@ def f(self, other):
|
79 | 79 |
|
80 | 80 | def maybe_to_categorical(array):
|
81 | 81 | """ coerce to a categorical if a series is given """
|
82 |
| - if isinstance(array, ABCSeries): |
| 82 | + if isinstance(array, (ABCSeries, ABCCategoricalIndex)): |
83 | 83 | return array.values
|
84 | 84 | return array
|
85 | 85 |
|
@@ -233,6 +233,10 @@ def __init__(self, values, categories=None, ordered=False, name=None, fastpath=F
|
233 | 233 | cat = values
|
234 | 234 | if isinstance(values, ABCSeries):
|
235 | 235 | cat = values.values
|
| 236 | + if isinstance(values, ABCCategoricalIndex): |
| 237 | + ordered = values.ordered |
| 238 | + cat = values.values |
| 239 | + |
236 | 240 | if categories is None:
|
237 | 241 | categories = cat.categories
|
238 | 242 | values = values.__array__()
|
@@ -302,11 +306,31 @@ def copy(self):
|
302 | 306 | return Categorical(values=self._codes.copy(),categories=self.categories,
|
303 | 307 | name=self.name, ordered=self.ordered, fastpath=True)
|
304 | 308 |
|
| 309 | + def astype(self, dtype): |
| 310 | + """ coerce this type to another dtype """ |
| 311 | + if is_categorical_dtype(dtype): |
| 312 | + return self |
| 313 | + elif is_object_dtype(dtype): |
| 314 | + return np.array(self) |
| 315 | + |
| 316 | + raise TypeError('Astype a Categorical to anything other than ' |
| 317 | + 'categorical or object is not supported') |
| 318 | + |
305 | 319 | @cache_readonly
|
306 | 320 | def ndim(self):
|
307 | 321 | """Number of dimensions of the Categorical """
|
308 | 322 | return self._codes.ndim
|
309 | 323 |
|
| 324 | + @cache_readonly |
| 325 | + def size(self): |
| 326 | + """ return the len of myself """ |
| 327 | + return len(self) |
| 328 | + |
| 329 | + @cache_readonly |
| 330 | + def itemsize(self): |
| 331 | + """ return the size of a single category """ |
| 332 | + return self.categories.itemsize |
| 333 | + |
310 | 334 | def reshape(self, new_shape, **kwargs):
|
311 | 335 | """ compat with .reshape """
|
312 | 336 | return self
|
|
0 commit comments