Skip to content

Commit 8ee9e76

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

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pandas/core/categorical.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,12 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
234234
fastpath=False):
235235

236236
if dtype is not None:
237-
if categories is not None or ordered is not None:
237+
if isinstance(dtype, compat.string_types) and dtype == 'category':
238+
dtype = CategoricalDtype(categories, ordered)
239+
elif categories is not None or ordered is not None:
238240
raise ValueError("Cannot specify both `dtype` and `categories`"
239241
" or `ordered`.")
242+
240243
categories = dtype.categories
241244
ordered = dtype.ordered
242245

pandas/tests/test_categorical.py

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ 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'])
460+
tm.assert_categorical_equal(result, expected)
461+
457462
def test_from_codes(self):
458463

459464
# too few categories

0 commit comments

Comments
 (0)