BUG: Interval should disallow Interval/Period objects as left/right endpoints · Issue #23013 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
Interval
with Interval
/Period
endpoints is allowed:
In [1]: import pandas as pd; pd.version Out[1]: '0.24.0.dev0+681.g02f19f8'
In [2]: left = pd.Period(f'2018Q1', freq='Q')
In [3]: right = pd.Period(f'2018Q4', freq='Q')
In [4]: iv = pd.Interval(left, right)
In [5]: iv Out[5]: Interval(Period('2018Q1', 'Q-DEC'), Period('2018Q4', 'Q-DEC'), closed='right')
In [6]: pd.Interval(iv, iv + 1) Out[6]: Interval(Interval(Period('2018Q1', 'Q-DEC'), Period('2018Q4', 'Q-DEC'), closed='right'), Interval(Period('2018Q2', 'Q-DEC'), Period('2019Q1', 'Q-DEC'), closed='right'), closed='right')
I don't think we should currently support this behavior. I can't think of much use for it, and it breaks some existing methods in certain cases:
In [7]: iv.mid
ValueError: n
argument must be an integer, got 1.5
Note that similar behavior has been disallowed for IntervalIndex
:
In [8]: pd.IntervalIndex.from_breaks(pd.period_range('2018Q1', freq='Q', periods=4))
ValueError: Period dtypes are not supported, use a PeriodIndex instead
I wouldn't necessarily be against allowing this behavior if there's a valid use case for it. I think it'd be non-trivial to support though.
Currently recommend restricting Interval
to numeric/Timestamp
/Timedelta
endpoints, as that's what's been most tested.
Output of pd.show_versions()
INSTALLED VERSIONS
commit: 02f19f8
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.29-galliumos
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.24.0.dev0+681.g02f19f8
pytest: 3.5.1
pip: 18.0
setuptools: 39.1.0
Cython: 0.28.2
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.3
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.4
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.7
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None