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

IntervalIndex.from_arrays/from_breaks does not accept categorical data with valid dtype #21253

Closed
jschendel opened this Issue May 30, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@jschendel
Member

jschendel commented May 30, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment