Skip to content

BUG: Interval should disallow Interval/Period objects as left/right endpoints #23013

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

Closed
jschendel opened this issue Oct 6, 2018 · 2 comments · Fixed by #25768
Closed

BUG: Interval should disallow Interval/Period objects as left/right endpoints #23013

jschendel opened this issue Oct 6, 2018 · 2 comments · Fixed by #25768
Labels
Interval Interval data type Period Period data type
Milestone

Comments

@jschendel
Copy link
Member

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

@jschendel jschendel added Period Period data type Interval Interval data type labels Oct 6, 2018
@jschendel jschendel added this to the Contributions Welcome milestone Oct 6, 2018
@pambot
Copy link
Contributor

pambot commented Oct 8, 2018

I was thinking of picking this up and putting in the same type checks in Interval that IntervalArray has, namely (modified as needed):

        # coerce dtypes to match if needed
        if is_float_dtype(left) and is_integer_dtype(right):
            right = right.astype(left.dtype)
        elif is_float_dtype(right) and is_integer_dtype(left):
            left = left.astype(right.dtype)

        if type(left) != type(right):
            msg = ('must not have differing left [{ltype}] and right '
                   '[{rtype}] types')
            raise ValueError(msg.format(ltype=type(left).__name__,
                                        rtype=type(right).__name__))
        elif is_categorical_dtype(left.dtype) or is_string_dtype(left.dtype):
            # GH 19016
            msg = ('category, object, and string subtypes are not supported '
                   'for IntervalArray')
            raise TypeError(msg)
        elif isinstance(left, ABCPeriodIndex):
            msg = 'Period dtypes are not supported, use a PeriodIndex instead'
            raise ValueError(msg)
        elif (isinstance(left, ABCDatetimeIndex) and
                str(left.tz) != str(right.tz)):
            msg = ("left and right must have the same time zone, got "
                   "'{left_tz}' and '{right_tz}'")
            raise ValueError(msg.format(left_tz=left.tz, right_tz=right.tz))

@jschendel
Copy link
Member Author

Yes, modifying the IntervalArray checks as needed should work. It might be more straightforward to put in checks that verify that the endpoints are valid types (numeric, timestamp, timedelta) than to have a series of checks that try catching invalid types. Either way should work though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Interval Interval data type Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants