|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | # pylint: disable=E1101,E1103,W0232
|
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | import sys
|
5 | 6 | from datetime import datetime
|
6 | 7 | from distutils.version import LooseVersion
|
|
17 | 18 | import pandas.compat as compat
|
18 | 19 | import pandas.util.testing as tm
|
19 | 20 | from pandas import (Categorical, Index, Series, DataFrame, PeriodIndex,
|
20 |
| - Timestamp, CategoricalIndex, isnull) |
| 21 | + Timestamp, CategoricalIndex, DatetimeIndex, |
| 22 | + isnull, NaT) |
21 | 23 | from pandas.compat import range, lrange, u, PY3
|
22 | 24 | from pandas.core.config import option_context
|
23 | 25 |
|
@@ -223,15 +225,6 @@ def f():
|
223 | 225 | # vals = np.asarray(cat[cat.notnull()])
|
224 | 226 | # self.assertTrue(is_integer_dtype(vals))
|
225 | 227 |
|
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 |
| - |
235 | 228 | # corner cases
|
236 | 229 | cat = pd.Categorical([1])
|
237 | 230 | self.assertTrue(len(cat.categories) == 1)
|
@@ -281,6 +274,22 @@ def f(null_value):
|
281 | 274 | c = Categorical(np.array([], dtype='int64'), # noqa
|
282 | 275 | categories=[3, 2, 1], ordered=True)
|
283 | 276 |
|
| 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 | + |
284 | 293 | def test_constructor_with_index(self):
|
285 | 294 | ci = CategoricalIndex(list('aabbca'), categories=list('cab'))
|
286 | 295 | tm.assert_categorical_equal(ci.values, Categorical(ci))
|
|
0 commit comments