|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from itertools import product
|
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | import numpy as np
|
5 | 6 | import pandas as pd
|
6 | 7 | from pandas import Series, Categorical, IntervalIndex, date_range
|
@@ -356,6 +357,70 @@ def test_not_string(self):
|
356 | 357 | self.assertFalse(is_string_dtype(PeriodDtype('D')))
|
357 | 358 |
|
358 | 359 |
|
| 360 | +class TestCategoricalDtypeParametrized(object): |
| 361 | + |
| 362 | + @pytest.mark.parametrize('categories, ordered', [ |
| 363 | + (['a', 'b', 'c', 'd'], False), |
| 364 | + (['a', 'b', 'c', 'd'], True), |
| 365 | + (np.arange(1000), False), |
| 366 | + (np.arange(1000), True), |
| 367 | + (['a', 'b', 10, 2, 1.3, True], False), |
| 368 | + ([True, False], True), |
| 369 | + ([True, False], False), |
| 370 | + (pd.date_range('2017', periods=4), True), |
| 371 | + (pd.date_range('2017', periods=4), False), |
| 372 | + ]) |
| 373 | + def test_basic(self, categories, ordered): |
| 374 | + c1 = CategoricalDtype(categories, ordered=ordered) |
| 375 | + tm.assert_index_equal(c1.categories, pd.Index(categories)) |
| 376 | + assert c1.ordered is ordered |
| 377 | + |
| 378 | + @pytest.mark.parametrize('ordered', [True, False]) |
| 379 | + def test_is_singleton(self, ordered): |
| 380 | + c1 = CategoricalDtype(['a', 'b', 'c'], ordered=ordered) |
| 381 | + c2 = CategoricalDtype(['a', 'b', 'c'], ordered=ordered) |
| 382 | + assert c1 is c2 |
| 383 | + |
| 384 | + def test_order_matters(self): |
| 385 | + categories = ['a', 'b'] |
| 386 | + c1 = CategoricalDtype(categories, ordered=False) |
| 387 | + c2 = CategoricalDtype(categories, ordered=True) |
| 388 | + assert c1 is not c2 |
| 389 | + |
| 390 | + def test_unordered_same(self): |
| 391 | + c1 = CategoricalDtype(['a', 'b']) |
| 392 | + c2 = CategoricalDtype(['b', 'a']) |
| 393 | + assert c1 is c2 |
| 394 | + tm.assert_index_equal(c1.categories, c2.categories) |
| 395 | + |
| 396 | + def test_categories(self): |
| 397 | + result = CategoricalDtype(['a', 'b', 'c']) |
| 398 | + tm.assert_index_equal(result.categories, pd.Index(['a', 'b', 'c'])) |
| 399 | + assert result.ordered is False |
| 400 | + |
| 401 | + def test_equal_but_different(self): |
| 402 | + c1 = CategoricalDtype([1, 2, 3]) |
| 403 | + c2 = CategoricalDtype([1., 2., 3.]) |
| 404 | + assert c1 is not c2 |
| 405 | + |
| 406 | + @pytest.mark.parametrize('v1, v2', [ |
| 407 | + ([1, 2, 3], [1, 2, 3]), |
| 408 | + ([1, 2, 3], [3, 2, 1]), |
| 409 | + ]) |
| 410 | + def test_order_hashes_different(self, v1, v2): |
| 411 | + c1 = CategoricalDtype(v1) |
| 412 | + c2 = CategoricalDtype(v2, ordered=True) |
| 413 | + assert c1 is not c2 |
| 414 | + |
| 415 | + def test_nan_invalid(self): |
| 416 | + with pytest.raises(ValueError): |
| 417 | + pd.CategoricalDtype([1, 2, np.nan]) |
| 418 | + |
| 419 | + def test_non_unique_invalid(self): |
| 420 | + with pytest.raises(ValueError): |
| 421 | + pd.CategoricalDtype([1, 2, 1]) |
| 422 | + |
| 423 | + |
359 | 424 | class TestIntervalDtype(Base, tm.TestCase):
|
360 | 425 |
|
361 | 426 | # TODO: placeholder
|
|
0 commit comments