Skip to content

Commit 65c66b0

Browse files
committed
Improve error messages
1 parent c9a0405 commit 65c66b0

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

pandas/core/dtypes/dtypes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pandas._libs.tslibs import NaT, Period, Timestamp, timezones
1010

1111
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndexClass
12+
from .inference import is_list_like
1213

1314
from pandas import compat
1415

@@ -408,7 +409,10 @@ def validate_categories(categories, fastpath=False):
408409
"""
409410
from pandas import Index
410411

411-
if not isinstance(categories, ABCIndexClass):
412+
if not fastpath and not is_list_like(categories, allow_sets=True):
413+
msg = "Parameter 'categories' must be list-like, was {!r}"
414+
raise TypeError(msg.format(categories))
415+
elif not isinstance(categories, ABCIndexClass):
412416
categories = Index(categories, tupleize_cols=False)
413417

414418
if not fastpath:

pandas/tests/dtypes/test_dtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_construction_from_string(self):
9090
TypeError, lambda: CategoricalDtype.construct_from_string('foo'))
9191

9292
def test_constructor_invalid(self):
93-
msg = "CategoricalIndex.* must be called"
93+
msg = "categories must be list-like"
9494
with pytest.raises(TypeError, match=msg):
9595
CategoricalDtype("category")
9696

@@ -706,7 +706,7 @@ def test_invalid_raises(self):
706706
with pytest.raises(TypeError, match='ordered'):
707707
CategoricalDtype(['a', 'b'], ordered='foo')
708708

709-
with pytest.raises(TypeError, match='collection'):
709+
with pytest.raises(TypeError, match='categories must be list-like'):
710710
CategoricalDtype('category')
711711

712712
def test_mixed(self):

0 commit comments

Comments
 (0)