Skip to content

Commit d7479a5

Browse files
committed
fixup! ENH: Parametrized CategoricalDtype
1 parent 8ee9e76 commit d7479a5

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

pandas/core/categorical.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,11 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
234234
fastpath=False):
235235

236236
if dtype is not None:
237-
if isinstance(dtype, compat.string_types) and dtype == 'category':
238-
dtype = CategoricalDtype(categories, ordered)
237+
if isinstance(dtype, compat.string_types):
238+
if dtype == 'category':
239+
dtype = CategoricalDtype(categories, ordered)
240+
else:
241+
raise ValueError("Unknown `dtype` {}".format(dtype))
239242
elif categories is not None or ordered is not None:
240243
raise ValueError("Cannot specify both `dtype` and `categories`"
241244
" or `ordered`.")

pandas/core/dtypes/dtypes.py

-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ def __unicode__(self):
215215
data = self.categories._format_data(name=self.__class__.__name__)
216216
return tpl.format(data, self.ordered)
217217

218-
def __repr__(self):
219-
return str(self)
220-
221218
@staticmethod
222219
def _hash_categories(categories, ordered=True):
223220
from pandas.core.util.hashing import (

pandas/core/indexes/category.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def _shallow_copy(self, values=None, categories=None, ordered=None,
180180
# overridden.
181181
if dtype is not None and (categories is not None or
182182
ordered is not None):
183-
raise TypeError
183+
raise TypeError("Cannot specify both `dtype` and `categories` "
184+
"or `ordered`")
184185

185186
if categories is None and ordered is None:
186187
dtype = self.dtype if dtype is None else dtype

pandas/tests/dtypes/test_common.py

+1
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ def test_is_complex_dtype():
550550
(pd.CategoricalIndex(['a', 'b']).dtype, CategoricalDtype(['a', 'b'])),
551551
(pd.CategoricalIndex(['a', 'b']), CategoricalDtype(['a', 'b'])),
552552
(CategoricalDtype(), CategoricalDtype()),
553+
(CategoricalDtype(['a', 'b']), CategoricalDtype()),
553554
(pd.DatetimeIndex([1, 2]), np.dtype('<M8[ns]')),
554555
(pd.DatetimeIndex([1, 2]).dtype, np.dtype('<M8[ns]')),
555556
('<M8[ns]', np.dtype('<M8[ns]')),

pandas/tests/test_categorical.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,21 @@ def test_constructor_dtype_and_others_raises(self):
454454
with tm.assert_raises_regex(ValueError, "Cannot"):
455455
Categorical(['a', 'b'], ordered=False, dtype=dtype)
456456

457-
def test_constructor_str_category(self):
458-
result = Categorical(['a', 'b'], dtype='category')
459-
expected = Categorical(['a', 'b'])
457+
@pytest.mark.parametrize('categories', [
458+
None, ['a', 'b'], ['a', 'c'],
459+
])
460+
@pytest.mark.parametrize('ordered', [True, False])
461+
def test_constructor_str_category(self, categories, ordered):
462+
result = Categorical(['a', 'b'], categories=categories,
463+
ordered=ordered, dtype='category')
464+
expected = Categorical(['a', 'b'], categories=categories,
465+
ordered=ordered)
460466
tm.assert_categorical_equal(result, expected)
461467

468+
def test_constructor_str_unknown(self):
469+
with tm.assert_raises_regex(ValueError, "Unknown `dtype`"):
470+
Categorical([1, 2], dtype="foo")
471+
462472
def test_from_codes(self):
463473

464474
# too few categories

0 commit comments

Comments
 (0)