Skip to content

BUG: Fix Index constructor with mixed closed Intervals #27173

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

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ Interval

- Construction of :class:`Interval` is restricted to numeric, :class:`Timestamp` and :class:`Timedelta` endpoints (:issue:`23013`)
- Fixed bug in :class:`Series`/:class:`DataFrame` not displaying ``NaN`` in :class:`IntervalIndex` with missing values (:issue:`25984`)
-
- Bug in :class:`Index` constructor where passing mixed closed :class:`Interval` objects would result in a ``ValueError`` instead of an ``object`` dtype ``Index`` (:issue:`27172`)

Indexing
^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,11 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
return Float64Index(subarr, copy=copy, name=name)
elif inferred == 'interval':
from .interval import IntervalIndex
return IntervalIndex(subarr, name=name, copy=copy)
try:
return IntervalIndex(subarr, name=name, copy=copy)
except ValueError:
# GH27172: mixed closed Intervals --> object dtype
pass
elif inferred == 'boolean':
# don't support boolean explicitly ATM
pass
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/interval/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ def test_index_object_dtype(self, values_constructor):
assert type(result) is Index
tm.assert_numpy_array_equal(result.values, np.array(values))

def test_index_mixed_closed(self):
# GH27172
intervals = [Interval(0, 1, closed='left'),
Interval(1, 2, closed='right'),
Interval(2, 3, closed='neither'),
Interval(3, 4, closed='both')]
result = Index(intervals)
expected = Index(intervals, dtype=object)
tm.assert_index_equal(result, expected)


class TestFromIntervals(TestClassConstructors):
"""
Expand All @@ -388,3 +398,7 @@ def test_deprecated(self):
@pytest.mark.skip(reason='parent class test that is not applicable')
def test_index_object_dtype(self):
pass

@pytest.mark.skip(reason='parent class test that is not applicable')
def test_index_mixed_closed(self):
pass
13 changes: 12 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
CategoricalIndex, DataFrame, DatetimeIndex, Float64Index, Int64Index,
PeriodIndex, RangeIndex, Series, TimedeltaIndex, UInt64Index, date_range,
isna, period_range)
from pandas.core.index import _get_combined_index, ensure_index_from_sequences
from pandas.core.index import (
_get_combined_index, ensure_index, ensure_index_from_sequences)
from pandas.core.indexes.api import Index, MultiIndex
from pandas.core.sorting import safe_sort
from pandas.tests.indexes.common import Base
Expand Down Expand Up @@ -2432,6 +2433,16 @@ def test_ensure_index_from_sequences(self, data, names, expected):
result = ensure_index_from_sequences(data, names)
tm.assert_index_equal(result, expected)

def test_ensure_index_mixed_closed_intervals(self):
# GH27172
intervals = [pd.Interval(0, 1, closed='left'),
pd.Interval(1, 2, closed='right'),
pd.Interval(2, 3, closed='neither'),
pd.Interval(3, 4, closed='both')]
result = ensure_index(intervals)
expected = Index(intervals, dtype=object)
tm.assert_index_equal(result, expected)


@pytest.mark.parametrize('opname', ['eq', 'ne', 'le', 'lt', 'ge', 'gt',
'add', 'radd', 'sub', 'rsub',
Expand Down