From 318175bddaeabdcf85df0b095c4def32a0417ba6 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Mon, 27 Mar 2017 08:05:57 -0400 Subject: [PATCH] TST: test pd.NaT with correct dtype --- pandas/core/categorical.py | 2 +- pandas/tests/test_categorical.py | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index c34dea4145e8c..632c24c33feb7 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -547,7 +547,7 @@ def _validate_categories(cls, categories, fastpath=False): # Categories cannot contain NaN. if categories.hasnans: - raise ValueError('Categorial categories cannot be NaN') + raise ValueError('Categorial categories cannot be null') # Categories must be unique. if not categories.is_unique: diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index 62abbb8cfbe01..8fd3c6324d48c 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # pylint: disable=E1101,E1103,W0232 +import pytest import sys from datetime import datetime from distutils.version import LooseVersion @@ -17,7 +18,8 @@ import pandas.compat as compat import pandas.util.testing as tm from pandas import (Categorical, Index, Series, DataFrame, PeriodIndex, - Timestamp, CategoricalIndex, isnull) + Timestamp, CategoricalIndex, DatetimeIndex, + isnull, NaT) from pandas.compat import range, lrange, u, PY3 from pandas.core.config import option_context @@ -223,15 +225,6 @@ def f(): # vals = np.asarray(cat[cat.notnull()]) # self.assertTrue(is_integer_dtype(vals)) - # Cannot have NaN in categories - def f(null_value): - pd.Categorical([null_value, "a", "b", "c"], - categories=[null_value, "a", "b", "c"]) - - self.assertRaises(ValueError, f, np.nan) - self.assertRaises(ValueError, f, pd.NaT) - self.assertRaises(ValueError, f, None) - # corner cases cat = pd.Categorical([1]) self.assertTrue(len(cat.categories) == 1) @@ -281,6 +274,22 @@ def f(null_value): c = Categorical(np.array([], dtype='int64'), # noqa categories=[3, 2, 1], ordered=True) + def test_constructor_with_null(self): + + # Cannot have NaN in categories + with pytest.raises(ValueError): + pd.Categorical([np.nan, "a", "b", "c"], + categories=[np.nan, "a", "b", "c"]) + + with pytest.raises(ValueError): + pd.Categorical([None, "a", "b", "c"], + categories=[None, "a", "b", "c"]) + + with pytest.raises(ValueError): + pd.Categorical(DatetimeIndex(['nat', '20160101']), + categories=[NaT, Timestamp('20160101')]) + + def test_constructor_with_index(self): ci = CategoricalIndex(list('aabbca'), categories=list('cab')) tm.assert_categorical_equal(ci.values, Categorical(ci))