Skip to content

Commit 1bfeb90

Browse files
jschendeljreback
authored andcommitted
ERR: Improve error message for cut with infinity and integer bins (#24327)
1 parent 6c8aabc commit 1bfeb90

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/core/reshape/tile.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
205205
rng = (nanops.nanmin(x), nanops.nanmax(x))
206206
mn, mx = [mi + 0.0 for mi in rng]
207207

208-
if mn == mx: # adjust end points before binning
208+
if np.isinf(mn) or np.isinf(mx):
209+
# GH 24314
210+
raise ValueError('cannot specify integer `bins` when input data '
211+
'contains infinity')
212+
elif mn == mx: # adjust end points before binning
209213
mn -= .001 * abs(mn) if mn != 0 else .001
210214
mx += .001 * abs(mx) if mx != 0 else .001
211215
bins = np.linspace(mn, mx, bins + 1, endpoint=True)

pandas/tests/reshape/test_cut.py

+11
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ def test_cut_not_1d_arg(arg, cut_func):
137137
cut_func(arg, 2)
138138

139139

140+
@pytest.mark.parametrize('data', [
141+
[0, 1, 2, 3, 4, np.inf],
142+
[-np.inf, 0, 1, 2, 3, 4],
143+
[-np.inf, 0, 1, 2, 3, 4, np.inf]])
144+
def test_int_bins_with_inf(data):
145+
# GH 24314
146+
msg = 'cannot specify integer `bins` when input data contains infinity'
147+
with pytest.raises(ValueError, match=msg):
148+
cut(data, bins=3)
149+
150+
140151
def test_cut_out_of_range_more():
141152
# see gh-1511
142153
name = "x"

0 commit comments

Comments
 (0)