Skip to content

Commit 71a5737

Browse files
committed
fixups for @shoyer comments
1 parent ea18491 commit 71a5737

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

pandas/core/categorical.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
is_categorical_dtype, is_integer_dtype, is_object_dtype,
1919
_possibly_infer_to_datetimelike, get_dtype_kinds,
2020
is_list_like, is_sequence, is_null_slice, is_bool,
21+
is_dtypes_equal,
2122
_ensure_platform_int, _ensure_object, _ensure_int64,
2223
_coerce_indexer_dtype, _values_from_object, take_1d)
2324
from pandas.util.terminal import get_terminal_size
@@ -242,7 +243,7 @@ def __init__(self, values, categories=None, ordered=False, name=None, fastpath=F
242243
values = values.__array__()
243244

244245
elif isinstance(values, Index):
245-
values = np.array(values)
246+
#values = np.array(values)
246247
ordered = True
247248

248249
else:
@@ -311,11 +312,7 @@ def astype(self, dtype):
311312
""" coerce this type to another dtype """
312313
if is_categorical_dtype(dtype):
313314
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)
319316

320317
@cache_readonly
321318
def ndim(self):
@@ -1621,14 +1618,20 @@ def _delegate_method(self, name, *args, **kwargs):
16211618
##### utility routines #####
16221619

16231620
def _get_codes_for_values(values, categories):
1624-
""""
1621+
"""
16251622
utility routine to turn values into codes given the specified categories
16261623
"""
16271624

16281625
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):
16301631
values = _ensure_object(values)
1632+
if is_object_dtype(categories):
16311633
categories = _ensure_object(categories)
1634+
16321635
(hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
16331636
t = hash_klass(len(categories))
16341637
t.map_locations(_values_from_object(categories))

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2453,7 +2453,7 @@ def is_dtypes_equal(source, target):
24532453

24542454
try:
24552455
return source == target
2456-
except:
2456+
except TypeError:
24572457

24582458
# invalid comparison
24592459
# object == category will hit this

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,7 @@ def _evaluate_compare(self, other):
23652365
return result
23662366
try:
23672367
return Index(result)
2368-
except: # pragma: no cover
2368+
except TypeError:
23692369
return result
23702370

23712371
return _evaluate_compare

pandas/tests/test_categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2696,7 +2696,7 @@ def test_astype_categorical(self):
26962696
tm.assert_categorical_equal(cat,cat.astype('category'))
26972697
tm.assert_almost_equal(np.array(cat),cat.astype('object'))
26982698

2699-
self.assertRaises(TypeError, lambda : cat.astype(float))
2699+
self.assertRaises(ValueError, lambda : cat.astype(float))
27002700

27012701
def test_to_records(self):
27022702

0 commit comments

Comments
 (0)