IntervalIndex.from_arrays/from_breaks does not accept categorical data with valid dtype · Issue #21253 · pandas-dev/pandas (original) (raw)

xref #21243

Code Sample, a copy-pastable example if possible

IntervalIndex.from_arrays fails:

In [2]: cat_l = pd.Categorical([0, 0, 1, 2, 2])

In [3]: cat_r = pd.Categorical([1, 3, 3, 3, 4])

In [4]: pd.IntervalIndex.from_arrays(cat_l, cat_r)

TypeError: category, object, and string subtypes are not supported for IntervalIndex

IntervalIndex.from_breaks fails:

In [5]: cat_breaks = pd.Categorical([0, 1, 2, 3])

In [6]: pd.IntervalIndex.from_breaks(cat_breaks)

TypeError: category, object, and string subtypes are not supported for IntervalIndex

IntervalIndex.from_tuples currently works, though is likely not tested:

In [7]: cat_tup = pd.Categorical([(0, 1), (0, 1), (0, 3), (1, 2)])

In [8]: pd.IntervalIndex.from_tuples(cat_tup) Out[8]: IntervalIndex([(0, 1], (0, 1], (0, 3], (1, 2]] closed='right', dtype='interval[int64]')

IntervalIndex.from_intervals is deprecated, but will fail in the same way as described in #21243 since it hits the same code path.

The above examples also fail with CategoricalIndex.

Categorical data that is not of the appropriate dtype, e.g. strings, should still not be supported.

Problem description

An IntervalIndex cannot be created from categorical data with the appropriate dtype. I doubt the failing examples shown above are very common, but #21243 seems like it'd be more common, and fixing it gives us this support for free.

Expected Output

I'd expect these to be successfully created (i.e. same result as building from the underlying list of data).

Output of pd.show_versions()

INSTALLED VERSIONS

commit: c85ab08
python: 3.6.1.final.0
python-bits: 64
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.24.0.dev0+42.gc85ab08
pytest: 3.1.2
pip: 9.0.1
setuptools: 39.0.1
Cython: 0.28.2
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 6.1.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.5
pandas_gbq: None
pandas_datareader: None