Repository: pandas-dev/pandas

Issue:
Numpy 'inf' values cause pandas.cut to fail
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
foo = pd.Series([1, 2, 3,])
bar = pd.Series([1, 2, 0])
baz = foo / bar
cut = pd.cut(baz, 8, duplicates='drop')

*** ValueError: missing values must be missing in the same location both left and right sides

```
#### Problem description

Having an 'inf' value in a Series seems to cause pandas.cut to fail with this error:
*** ValueError: missing values must be missing in the same location both left and right sides

I saw bug #19768 already, but that was fixed by PR 19833 in Feb, and I'm using 0.23.4 which was released on August 3, 2018. Also there's #5483, which was fixed a long time ago.

#### Expected Output

'inf' should probably be similar to the current NA-handling behavior: it should at least not raise an exception, and just drop that as a usable value.

#### Output of ``pd.show_versions()``

<details>

pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: 3.10.0
pip: 18.1
setuptools: 40.2.0
Cython: 0.29.2
numpy: 1.15.1
scipy: 1.1.0
pyarrow: 0.11.0
xarray: None
IPython: 6.5.0
sphinx: 1.8.2
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: 2.5.10
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: 4.2.4
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py
--- a/pandas/core/reshape/tile.py
+++ b/pandas/core/reshape/tile.py
@@ -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)


Test patch:
diff --git a/pandas/tests/reshape/test_cut.py b/pandas/tests/reshape/test_cut.py
--- a/pandas/tests/reshape/test_cut.py
+++ b/pandas/tests/reshape/test_cut.py
@@ -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"