Skip to content

Commit

Permalink
TST: test pd.NaT with correct dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Mar 27, 2017
1 parent 4dce349 commit 318175b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 19 additions & 10 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 318175b

Please sign in to comment.