Skip to content

Commit 318175b

Browse files
committed
TST: test pd.NaT with correct dtype
1 parent 4dce349 commit 318175b

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pandas/core/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def _validate_categories(cls, categories, fastpath=False):
547547

548548
# Categories cannot contain NaN.
549549
if categories.hasnans:
550-
raise ValueError('Categorial categories cannot be NaN')
550+
raise ValueError('Categorial categories cannot be null')
551551

552552
# Categories must be unique.
553553
if not categories.is_unique:

pandas/tests/test_categorical.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# pylint: disable=E1101,E1103,W0232
33

4+
import pytest
45
import sys
56
from datetime import datetime
67
from distutils.version import LooseVersion
@@ -17,7 +18,8 @@
1718
import pandas.compat as compat
1819
import pandas.util.testing as tm
1920
from pandas import (Categorical, Index, Series, DataFrame, PeriodIndex,
20-
Timestamp, CategoricalIndex, isnull)
21+
Timestamp, CategoricalIndex, DatetimeIndex,
22+
isnull, NaT)
2123
from pandas.compat import range, lrange, u, PY3
2224
from pandas.core.config import option_context
2325

@@ -223,15 +225,6 @@ def f():
223225
# vals = np.asarray(cat[cat.notnull()])
224226
# self.assertTrue(is_integer_dtype(vals))
225227

226-
# Cannot have NaN in categories
227-
def f(null_value):
228-
pd.Categorical([null_value, "a", "b", "c"],
229-
categories=[null_value, "a", "b", "c"])
230-
231-
self.assertRaises(ValueError, f, np.nan)
232-
self.assertRaises(ValueError, f, pd.NaT)
233-
self.assertRaises(ValueError, f, None)
234-
235228
# corner cases
236229
cat = pd.Categorical([1])
237230
self.assertTrue(len(cat.categories) == 1)
@@ -281,6 +274,22 @@ def f(null_value):
281274
c = Categorical(np.array([], dtype='int64'), # noqa
282275
categories=[3, 2, 1], ordered=True)
283276

277+
def test_constructor_with_null(self):
278+
279+
# Cannot have NaN in categories
280+
with pytest.raises(ValueError):
281+
pd.Categorical([np.nan, "a", "b", "c"],
282+
categories=[np.nan, "a", "b", "c"])
283+
284+
with pytest.raises(ValueError):
285+
pd.Categorical([None, "a", "b", "c"],
286+
categories=[None, "a", "b", "c"])
287+
288+
with pytest.raises(ValueError):
289+
pd.Categorical(DatetimeIndex(['nat', '20160101']),
290+
categories=[NaT, Timestamp('20160101')])
291+
292+
284293
def test_constructor_with_index(self):
285294
ci = CategoricalIndex(list('aabbca'), categories=list('cab'))
286295
tm.assert_categorical_equal(ci.values, Categorical(ci))

0 commit comments

Comments
 (0)