Skip to content

BUG: prevent overflowing diffs raising error in cut (#26045) #26063

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 6 commits into from
Apr 19, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ Reshaping
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
- Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`)
- Bug in :func:`pandas.cut` where large bins could incorrectly raise an error due to an integer overflow (:issue:`26045`)

Sparse
^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
else:
bins = np.asarray(bins)
bins = _convert_bin_to_numeric_type(bins, dtype)
if (np.diff(bins) < 0).any():

# GH 26045: cast to float64 to avoid an overflow
if (np.diff(bins.astype('float64')) < 0).any():
raise ValueError('bins must increase monotonically.')

fac, bins = _bins_to_cuts(x, bins, right=right, labels=labels,
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/reshape/test_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ def test_bins_not_monotonic():
cut(data, [0.1, 1.5, 1, 10])


@pytest.mark.parametrize("x, bins, expected", [
(date_range("2017-12-31", periods=3),
[Timestamp.min, Timestamp('2018-01-01'), Timestamp.max],
IntervalIndex.from_tuples([
(Timestamp.min, Timestamp('2018-01-01')),
(Timestamp('2018-01-01'), Timestamp.max)])),

([-1, 0, 1],
np.array([np.iinfo(np.int64).min, 0, np.iinfo(np.int64).max],
dtype="int64"),
IntervalIndex.from_tuples([
(np.iinfo(np.int64).min, 0),
(0, np.iinfo(np.int64).max)])),

([np.timedelta64(-1), np.timedelta64(0), np.timedelta64(1)],
np.array([
np.timedelta64(-np.iinfo(np.int64).max),
np.timedelta64(0),
np.timedelta64(np.iinfo(np.int64).max)]),
IntervalIndex.from_tuples([
(np.timedelta64(-np.iinfo(np.int64).max), np.timedelta64(0)),
(np.timedelta64(0), np.timedelta64(np.iinfo(np.int64).max))])),
])
def test_bins_monotonic_not_overflowing(x, bins, expected):
# GH 26045
result = cut(x, bins)
tm.assert_index_equal(result.categories, expected)


def test_wrong_num_labels():
msg = "Bin labels must be one fewer than the number of bin edges"
data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
Expand Down