Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,7 @@ Reshaping
- Bug in :func:`pandas.melt` when passing column names that are not present in ``DataFrame`` (:issue:`23575`)
- Bug in :meth:`DataFrame.append` with a :class:`Series` with a dateutil timezone would raise a ``TypeError`` (:issue:`23682`)
- Bug in ``Series`` construction when passing no data and ``dtype=str`` (:issue:`22477`)
- Bug in :func:`cut` with ``bins`` as an overlapping ``IntervalIndex`` where multiple bins were returned per item instead of raising a ``ValueError`` (:issue:`23980`)

.. _whatsnew_0240.bug_fixes.sparse:

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
and maximum values of `x`.
* sequence of scalars : Defines the bin edges allowing for non-uniform
width. No extension of the range of `x` is done.
* IntervalIndex : Defines the exact bins to be used.
* IntervalIndex : Defines the exact bins to be used. Note that IntervalIndex
for `bins` must be non-overlapping.

right : bool, default True
Indicates whether `bins` includes the rightmost edge or not. If
Expand Down Expand Up @@ -217,7 +218,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
6 changes: 6 additions & 0 deletions pandas/tests/reshape/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ 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):
# verify if issue 23980 is properly solved.
ii = IntervalIndex.from_tuples([(0, 10), (2, 12), (4, 14)])
with pytest.raises(ValueError):
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