Skip to content

ERR: Improve error message for cut with infinity in input and integer bins #24327

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 1 commit into from
Dec 18, 2018
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
6 changes: 5 additions & 1 deletion pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
rng = (nanops.nanmin(x), nanops.nanmax(x))
mn, mx = [mi + 0.0 for mi in rng]

if mn == mx: # adjust end points before binning
if np.isinf(mn) or np.isinf(mx):
# GH 24314
raise ValueError('cannot specify integer `bins` when input data '
'contains infinity')
elif mn == mx: # adjust end points before binning
mn -= .001 * abs(mn) if mn != 0 else .001
mx += .001 * abs(mx) if mx != 0 else .001
bins = np.linspace(mn, mx, bins + 1, endpoint=True)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/reshape/test_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ def test_cut_not_1d_arg(arg, cut_func):
cut_func(arg, 2)


@pytest.mark.parametrize('data', [
[0, 1, 2, 3, 4, np.inf],
[-np.inf, 0, 1, 2, 3, 4],
[-np.inf, 0, 1, 2, 3, 4, np.inf]])
def test_int_bins_with_inf(data):
# GH 24314
msg = 'cannot specify integer `bins` when input data contains infinity'
with pytest.raises(ValueError, match=msg):
cut(data, bins=3)


def test_cut_out_of_range_more():
# see gh-1511
name = "x"
Expand Down