Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ Bug Fixes
- Bug in ``value_counts`` when ``normalize=True`` and ``dropna=True`` where nulls still contributed to the normalized count (:issue:`12558`)

- Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`)

- Bug in ``Series`` when Series with Categorical is created with dtype specified (:issue:`12574`)
7 changes: 5 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
else:
data = data.reindex(index, copy=copy)
elif isinstance(data, Categorical):
if dtype is not None:
# GH12574: Allow dtype=category only, otherwise error
if ((dtype is not None) and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not right here; use is_categorical_dtype(dtype)

not is_categorical_dtype(dtype)):
raise ValueError("cannot specify a dtype with a "
"Categorical")
"Categorical unless "
"dtype='category'")
elif (isinstance(data, types.GeneratorType) or
(compat.PY3 and isinstance(data, map))):
data = list(data)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ def test_constructor_categorical(self):
res = Series(cat)
self.assertTrue(res.values.equals(cat))

# GH12574
self.assertRaises(
ValueError, lambda: Series(pd.Categorical([1, 2, 3]),
dtype='int64'))
cat = Series(pd.Categorical([1, 2, 3]), dtype='category')
self.assertTrue(com.is_categorical_dtype(cat))
self.assertTrue(com.is_categorical_dtype(cat.dtype))
s = Series([1, 2, 3], dtype='category')
self.assertTrue(com.is_categorical_dtype(s))
self.assertTrue(com.is_categorical_dtype(s.dtype))

def test_constructor_maskedarray(self):
data = ma.masked_all((3, ), dtype=float)
result = Series(data)
Expand Down