Skip to content

Commit 9f76e40

Browse files
committed
BUG: prevent overflowing diffs raising error in cut (pandas-dev#26045)
1 parent e02ec8f commit 9f76e40

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pandas/core/reshape/tile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
230230
else:
231231
bins = np.asarray(bins)
232232
bins = _convert_bin_to_numeric_type(bins, dtype)
233-
if (np.diff(bins) < 0).any():
233+
if (np.diff(bins.astype('float64')) < 0).any():
234234
raise ValueError('bins must increase monotonically.')
235235

236236
fac, bins = _bins_to_cuts(x, bins, right=right, labels=labels,

pandas/tests/reshape/test_cut.py

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ def test_bins_not_monotonic():
112112
cut(data, [0.1, 1.5, 1, 10])
113113

114114

115+
def test_bins_monotic_not_overflowing():
116+
data = date_range("2017-12-31", periods=3)
117+
118+
result = cut(data, [Timestamp.min, Timestamp('2018-01-01'), Timestamp.max])
119+
tm.assert_numpy_array_equal(result.codes,
120+
np.array([0, 0, 1], dtype="int8"))
121+
122+
115123
def test_wrong_num_labels():
116124
msg = "Bin labels must be one fewer than the number of bin edges"
117125
data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]

0 commit comments

Comments
 (0)