Skip to content

BUG: error thrown reindexing empty CategoricalIndex #16770

Closed
@adbull

Description

@adbull

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> index = pd.CategoricalIndex([], [0])

>>> pd.Series(index=index).reindex(index=[0])
Traceback (most recent call last):
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2733, in reindex
    **kwargs)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/generic.py", line 2515, in reindex
    fill_value, copy).__finalize__(self)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2679, in _reindex_axes
    fill_value, limit, tolerance)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2687, in _reindex_index
    tolerance=tolerance)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/category.py", line 422, in reindex
    new_target = self.take(indexer)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/category.py", line 560, in take
    na_value=-1)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 1806, in _assert_take_fillable
    taken = values.take(indices)
IndexError: cannot do a non-empty take from an empty axes.

>>> pd.DataFrame(index=index).reindex(index=[0])
Traceback (most recent call last):
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2733, in reindex
    **kwargs)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/generic.py", line 2515, in reindex
    fill_value, copy).__finalize__(self)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2679, in _reindex_axes
    fill_value, limit, tolerance)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2687, in _reindex_index
    tolerance=tolerance)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/category.py", line 422, in reindex
    new_target = self.take(indexer)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/category.py", line 560, in take
    na_value=-1)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 1806, in _assert_take_fillable
    taken = values.take(indices)
IndexError: cannot do a non-empty take from an empty axes.

>>> pd.DataFrame(columns=index).reindex(columns=[0])
Traceback (most recent call last):
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2733, in reindex
    **kwargs)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/generic.py", line 2515, in reindex
    fill_value, copy).__finalize__(self)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2679, in _reindex_axes
    fill_value, limit, tolerance)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/frame.py", line 2687, in _reindex_index
    tolerance=tolerance)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/category.py", line 422, in reindex
    new_target = self.take(indexer)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/category.py", line 560, in take
    na_value=-1)
  File "~/miniconda3/envs/pantheon/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 1806, in _assert_take_fillable
    taken = values.take(indices)
IndexError: cannot do a non-empty take from an empty axes.

Problem description

Reindexing a series or frame with an empty CategoricalIndex throws an error. This behaviour is inconsistent with other indexes, which can reindex an empty series or frame without error. While it is of course possible to work around this issue, it requires unnecessarily complex code when the size of the input is not known in advance.

Expected Output

No errors thrown, objects reindexed.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.10-100.fc24.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C
LANG: C
LOCALE: None.None

pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 33.1.1.post20170320
Cython: 0.25.2
numpy: 1.13.0
scipy: 0.19.0
xarray: None
IPython: 4.2.1
sphinx: None
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.5
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCategoricalCategorical Data TypeIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions