Skip to content

Setting CategoricalIndex with dtype=CategoricalDtype doesn't work (v0.21) #18109

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

Closed
topper-123 opened this issue Nov 4, 2017 · 1 comment · Fixed by #18116
Closed

Setting CategoricalIndex with dtype=CategoricalDtype doesn't work (v0.21) #18109

topper-123 opened this issue Nov 4, 2017 · 1 comment · Fixed by #18116
Labels
Bug Categorical Categorical Data Type
Milestone

Comments

@topper-123
Copy link
Contributor

topper-123 commented Nov 4, 2017

Code Sample, a copy-pastable example if possible

>>> c = pd.api.types.CategoricalDtype('c b a'.split(), ordered=True)
>>> c
CategoricalDtype(categories=['c', 'b', 'a'], ordered=True)
>>> pd.CategoricalIndex('a b b c'.split(), dtype=c)
CategoricalIndex(['a', 'b', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')

Note that categories and ordered above do not follow the rules from c.

Problem description

Using pd.Categorical(..., dtype=c) works in v0.21, so IMO pd.CategoricalIndex(..., dtype=c) should work as well, but deosn't.

Expected Output

>>> pd.CategoricalIndex('a b b c'.split(), dtype=c)
CategoricalIndex(['a', 'b', 'b', 'c'], categories=['c', 'b', 'a'], ordered=True, dtype='category')

Note that categories and ordered do follow the rules from c in this expected version.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 32
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.21.0
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.5.0.post20170922
Cython: None
numpy: 1.11.3
scipy: None
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: 2.4.8
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Nov 4, 2017

dtype needs to be passed thru here: https://github.com/pandas-dev/pandas/blob/master/pandas/core/indexes/category.py#L82

this case is only for list-likes (but should make sure have coverage for passing in CategoricalIndex/Categorical)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants