|
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
|
@@ -242,7 +243,7 @@ def __init__(self, values, categories=None, ordered=False, name=None, fastpath=F
|
242 | 243 | values = values.__array__()
|
243 | 244 |
|
244 | 245 | elif isinstance(values, Index):
|
245 |
| - values = np.array(values) |
| 246 | + #values = np.array(values) |
246 | 247 | ordered = True
|
247 | 248 |
|
248 | 249 | else:
|
@@ -311,11 +312,7 @@ def astype(self, dtype):
|
311 | 312 | """ coerce this type to another dtype """
|
312 | 313 | if is_categorical_dtype(dtype):
|
313 | 314 | return self
|
314 |
| - elif is_object_dtype(dtype): |
315 |
| - return np.array(self) |
316 |
| - |
317 |
| - raise TypeError('Astype a Categorical to anything other than ' |
318 |
| - 'categorical or object is not supported') |
| 315 | + return np.array(self, dtype=dtype) |
319 | 316 |
|
320 | 317 | @cache_readonly
|
321 | 318 | def ndim(self):
|
@@ -1621,14 +1618,20 @@ def _delegate_method(self, name, *args, **kwargs):
|
1621 | 1618 | ##### utility routines #####
|
1622 | 1619 |
|
1623 | 1620 | def _get_codes_for_values(values, categories):
|
1624 |
| - """" |
| 1621 | + """ |
1625 | 1622 | utility routine to turn values into codes given the specified categories
|
1626 | 1623 | """
|
1627 | 1624 |
|
1628 | 1625 | from pandas.core.algorithms import _get_data_algo, _hashtables
|
1629 |
| - 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): |
1630 | 1631 | values = _ensure_object(values)
|
| 1632 | + if is_object_dtype(categories): |
1631 | 1633 | categories = _ensure_object(categories)
|
| 1634 | + |
1632 | 1635 | (hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
|
1633 | 1636 | t = hash_klass(len(categories))
|
1634 | 1637 | t.map_locations(_values_from_object(categories))
|
|
0 commit comments