Skip to content
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

BUG: pd.IntervalIndex.difference raises when empty difference #19101

Closed
topper-123 opened this Issue Jan 6, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@topper-123
Copy link
Contributor

commented Jan 6, 2018

Code Sample, a copy-pastable example if possible

>>> ii1 = pd.IntervalIndex.from_breaks([0,1,2])
>>> ii2 = pd.IntervalIndex.from_breaks([0,1])
>>> ii2.difference(ii1)
TypeError: category, object, and string subtypes are not supported for IntervalIndex

Problem description

A test in my PR #19021 that passed a few days ago, is now failing. The issue is AFAIKT that IntervalIndex.difference incorrectly casts an empy difference as having dtype object, where it should have dtype Interval[int64].

xref #19022, that seems to have surfaced if not caused this bug.

Expected Output

Expected was IntervalIndex([] closed='right', dtype='interval[int64]').

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 26e7419
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.22.0.dev0+511.g26e7419
pytest: 3.3.1
pip: 9.0.1
setuptools: 38.2.5
Cython: 0.26.1
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0b10
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@topper-123 topper-123 changed the title BUG: pd.IntervalIndex.difference raises when empty BUG: pd.IntervalIndex.difference raises when empty difference Jan 6, 2018

@gfyoung gfyoung added the Interval label Jan 6, 2018

@jschendel

This comment has been minimized.

Copy link
Member

commented Jan 6, 2018

Thanks, this appears to happen with any type of set operation that returns empty:

In [15]: ii1 = pd.interval_range(0, 4)

In [16]: ii2 = pd.interval_range(5, 10)

In [17]: ii1.intersection(ii2)
---------------------------------------------------------------------------
TypeError: category, object, and string subtypes are not supported for IntervalIndex

Under the hood, IntervalIndex uses MultiIndex set ops, and then takes the resulting array of tuples to build the result. When the result is empty, an empty object dtype array is returned by MultiIndex, so the inferred dtype is object, which is then rejected upon IntervalIndex creation.

Will submit a fix either later today or tomorrow.

@jschendel

This comment has been minimized.

Copy link
Member

commented Jan 7, 2018

It looks like there was a related bug in previous versions where dtype wasn't maintained for empty results:

In [2]: pd.__version__
Out[2]: '0.22.0'

In [3]: ii = pd.interval_range(0, 4)

In [4]: ii.dtype
Out[4]: interval[int64]

In [5]: ii.difference(ii)
Out[5]:
IntervalIndex([]
              closed='right',
              dtype='interval[float64]')

Seems like the returned dtype should match what a non-empty result would have.

@jreback jreback added this to the 0.23.0 milestone Jan 7, 2018

@jreback jreback added the Bug label Jan 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.