Skip to content

BUG: Let CategoricalIndex take CategoricalDtype as dtype argument #18116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Categorical
- Bug in :meth:`DataFrame.astype` where casting to 'category' on an empty ``DataFrame`` causes a segmentation fault (:issue:`18004`)
- Error messages in the testing module have been improved when items have
different ``CategoricalDtype`` (:issue:`18069`)
-
- ``CategoricalIndex`` can now correctly take a ``pd.api.types.CategoricalDtype`` as its dtype (:issue:`18116`)

Other
^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
if data is not None or categories is None:
cls._scalar_data_error(data)
data = []
data = cls._create_categorical(cls, data, categories, ordered)
data = cls._create_categorical(cls, data, categories, ordered,
dtype)

if copy:
data = data.copy()
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pandas.util.testing as tm
from pandas.core.indexes.api import Index, CategoricalIndex
from pandas.core.dtypes.dtypes import CategoricalDtype
from .common import Base

from pandas.compat import range, PY3
Expand Down Expand Up @@ -95,6 +96,11 @@ def test_construction(self):
1, -1, 0], dtype='int8'))
assert result.ordered

result = pd.CategoricalIndex(ci, categories=list('ab'), ordered=True)
expected = pd.CategoricalIndex(ci, categories=list('ab'), ordered=True,
dtype='category')
tm.assert_index_equal(result, expected, exact=True)

# turn me to an Index
result = Index(np.array(ci))
assert isinstance(result, Index)
Expand Down Expand Up @@ -125,6 +131,25 @@ def test_construction_with_dtype(self):
result = CategoricalIndex(idx, categories=idx, ordered=True)
tm.assert_index_equal(result, expected, exact=True)

def test_construction_with_categorical_dtype(self):
# construction with CategoricalDtype
# GH18109
data, cats, ordered = 'a a b b'.split(), 'c b a'.split(), True
dtype = CategoricalDtype(categories=cats, ordered=ordered)

result = pd.CategoricalIndex(data, dtype=dtype)
expected = pd.CategoricalIndex(data, categories=cats,
ordered=ordered)
tm.assert_index_equal(result, expected, exact=True)

# error to combine categories or ordered and dtype keywords args
with pytest.raises(ValueError, match="Cannot specify both `dtype` and "
"`categories` or `ordered`."):
pd.CategoricalIndex(data, categories=cats, dtype=dtype)
with pytest.raises(ValueError, match="Cannot specify both `dtype` and "
"`categories` or `ordered`."):
pd.CategoricalIndex(data, ordered=ordered, dtype=dtype)

def test_create_categorical(self):
# https://github.com/pandas-dev/pandas/pull/17513
# The public CI constructor doesn't hit this code path with
Expand Down