diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 1e3c8f89c0e05..b55c02a166754 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -308,6 +308,9 @@ def __init__(self, values, categories=None, ordered=None, dtype=None, elif isinstance(values, (ABCIndexClass, ABCSeries)): # we'll do inference later pass + elif getattr(values, 'ndim', 0) > 1: + raise NotImplementedError("> 1 ndim Categorical are not " + "supported at this time") else: @@ -2331,6 +2334,8 @@ def _factorize_from_iterable(values): categories=values.categories, ordered=values.ordered) codes = values.codes + elif getattr(values, 'ndim', 0) > 1: + raise NotImplementedError('Factorizing DataFrame is not supported.') else: cat = Categorical(values, ordered=True) categories = cat.categories diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index b77e2d1dcda8a..d6221cc48bc1e 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -173,6 +173,12 @@ def test_constructor_unsortable(self): pytest.raises( TypeError, lambda: Categorical(arr, ordered=True)) + def test_constructor_dataframe_error(self): + # GH#17112 + df = pd.DataFrame(np.random.randn(3, 2), columns=['A', 'B']) + with pytest.raises(NotImplementedError): + Categorical(df) + def test_constructor_interval(self): result = Categorical([Interval(1, 2), Interval(2, 3), Interval(3, 6)], ordered=True)