Skip to content

BUG: pandas.cut should disallow overlapping IntervalIndex bins #23999

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
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
bins[-1] += adj

elif isinstance(bins, IntervalIndex):
pass
if bins.is_overlapping:
raise ValueError('Overlapping IntervalIndex is not accepted.')

else:
bins = np.asarray(bins)
bins = _convert_bin_to_numeric_type(bins, dtype)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/reshape/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def test_bins_from_intervalindex(self):
tm.assert_numpy_array_equal(result.codes,
np.array([1, 1, 2], dtype='int8'))

def test_bins_not_overlapping_from_intervalindex(self):
ii = pd.IntervalIndex.from_tuples([(0, 10), (2, 12), (4, 14)])
with pytest.raises(ValueError):
pd.cut([5, 6], bins=ii)

def test_bins_not_monotonic(self):
data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
pytest.raises(ValueError, cut, data, [0.1, 1.5, 1, 10])
Expand Down