|
5 | 5 |
|
6 | 6 | import pandas as pd
|
7 | 7 | from pandas import (
|
| 8 | + CategoricalDtype, |
| 9 | + CategoricalIndex, |
8 | 10 | Float64Index,
|
9 | 11 | Index,
|
10 | 12 | Int64Index,
|
@@ -533,3 +535,28 @@ def test_isin_range(self, base):
|
533 | 535 | result = base.isin(values)
|
534 | 536 | expected = np.array([True, False])
|
535 | 537 | tm.assert_numpy_array_equal(result, expected)
|
| 538 | + |
| 539 | + @pytest.mark.parametrize("copy", [True, False]) |
| 540 | + @pytest.mark.parametrize("name", [None, "foo"]) |
| 541 | + @pytest.mark.parametrize("ordered", [True, False]) |
| 542 | + def test_astype_category(self, copy, name, ordered, simple_index): |
| 543 | + super().test_astype_category(copy, name, ordered, simple_index) |
| 544 | + |
| 545 | + # GH 41263 |
| 546 | + idx = simple_index |
| 547 | + if name: |
| 548 | + idx = idx.rename(name) |
| 549 | + |
| 550 | + # standard categories |
| 551 | + dtype = CategoricalDtype(ordered=ordered) |
| 552 | + result = idx.astype(dtype, copy=copy) |
| 553 | + assert isinstance(result, CategoricalIndex) |
| 554 | + assert isinstance(result.categories, RangeIndex) |
| 555 | + assert (result.categories == idx).all() |
| 556 | + |
| 557 | + if ordered is False: |
| 558 | + # dtype='category' defaults to ordered=False, so only test once |
| 559 | + result = idx.astype("category", copy=copy) |
| 560 | + assert isinstance(result, CategoricalIndex) |
| 561 | + assert isinstance(result.categories, RangeIndex) |
| 562 | + assert (result.categories == idx).all() |
0 commit comments