IntervalIndex.symmetric_difference doesn't handle a non-IntervalIndex correctly · Issue #18475 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
In [2]: idx1 = pd.IntervalIndex.from_breaks(range(5)) ...: idx1 ...: Out[2]: IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4]] closed='right', dtype='interval[int64]')
In [3]: idx2 = pd.Index([2, 3]) ...: idx2 ...: Out[3]: Int64Index([2, 3], dtype='int64')
In [4]: idx1.symmetric_difference(idx2) Out[4]: IntervalIndex([(0, 1], (3, 4]] closed='right', dtype='interval[int64]')
Seems to be working properly between two IntervalIndex
:
In [5]: idx1[:-1].symmetric_difference(idx1[1:]) Out[5]: IntervalIndex([(0, 1], (3, 4]] closed='right', dtype='interval[int64]')
Problem description
Set ops between an IntervalIndex
and non-IntervalIndex
should raise.
Seems to be caused by a typo:
symmetric_differnce = _setop('symmetric_difference') |
---|
Using the typo version raises:
In [6]: idx1.symmetric_differnce(idx2)
ValueError: can only do set operations between two IntervalIndex objects that are closed on the same side
The typo version also seems to have better perf:
In [11]: idx3 = pd.IntervalIndex.from_breaks(range(10**5))
In [12]: %timeit idx3[:-1].symmetric_difference(idx3[1:]) 1.25 s ± 23.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [13]: %timeit idx3[:-1].symmetric_differnce(idx3[1:]) 153 ms ± 6.54 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Output of pd.show_versions()
INSTALLED VERSIONS ------------------ commit: 412988epython: 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.22.0.dev0+213.g412988e
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.13.1
scipy: 0.19.1
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: None
tables: 3.4.2
numexpr: 2.6.2
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.0
pandas_gbq: None
pandas_datareader: None