Skip to content

Commit a7b4c65

Browse files
committed
Improve error messages
1 parent 6b31abd commit a7b4c65

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

@@ -403,7 +404,10 @@ def validate_categories(categories, fastpath=False):
403404
"""
404405
from pandas import Index
405406

406-
if not isinstance(categories, ABCIndexClass):
407+
if not fastpath and not is_list_like(categories, allow_sets=True):
408+
msg = "Parameter 'categories' must be list-like, was {!r}"
409+
raise TypeError(msg.format(categories))
410+
elif not isinstance(categories, ABCIndexClass):
407411
categories = Index(categories, tupleize_cols=False)
408412

409413
if not fastpath:

pandas/tests/dtypes/test_dtypes.py

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

9696
def test_constructor_invalid(self):
97-
msg = "CategoricalIndex.* must be called"
97+
msg = "categories must be list-like"
9898
with pytest.raises(TypeError, match=msg):
9999
CategoricalDtype("category")
100100

@@ -710,7 +710,7 @@ def test_invalid_raises(self):
710710
with pytest.raises(TypeError, match='ordered'):
711711
CategoricalDtype(['a', 'b'], ordered='foo')
712712

713-
with pytest.raises(TypeError, match='collection'):
713+
with pytest.raises(TypeError, match='categories must be list-like'):
714714
CategoricalDtype('category')
715715

716716
def test_mixed(self):

0 commit comments

Comments
 (0)