diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index dc11dd17bfdd7..92fd8fbdc3b4d 100644 --- a/doc/source/whatsnew/v0.19.2.txt +++ b/doc/source/whatsnew/v0.19.2.txt @@ -25,3 +25,4 @@ Bug Fixes - compat with ``dateutil==2.6.0`` for testing (:issue:`14621`) - allow ``nanoseconds`` in ``Timestamp.replace`` kwargs (:issue:`14621`) +- Bug in ``pd.cut`` (:issue:`14652`) diff --git a/pandas/tools/tests/test_tile.py b/pandas/tools/tests/test_tile.py index 16731620a1dcd..e5b9c65b515d6 100644 --- a/pandas/tools/tests/test_tile.py +++ b/pandas/tools/tests/test_tile.py @@ -271,6 +271,18 @@ def test_series_retbins(self): np.array([0, 0, 1, 1], dtype=np.int8)) tm.assert_numpy_array_equal(bins, np.array([0, 1.5, 3])) + def test_single_bin(self): + # issue 14652 + expected = Series([0, 0]) + + s = Series([9., 9.]) + result = cut(s, 1, labels=False) + tm.assert_series_equal(result, expected) + + s = Series([-9., -9.]) + result = cut(s, 1, labels=False) + tm.assert_series_equal(result, expected) + def curpath(): pth, _ = os.path.split(os.path.abspath(__file__)) diff --git a/pandas/tools/tile.py b/pandas/tools/tile.py index 62bbfc2f630a5..ef75f2f84779b 100644 --- a/pandas/tools/tile.py +++ b/pandas/tools/tile.py @@ -98,8 +98,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, mn, mx = [mi + 0.0 for mi in rng] if mn == mx: # adjust end points before binning - mn -= .001 * mn - mx += .001 * mx + mn -= .001 * abs(mn) + mx += .001 * abs(mx) bins = np.linspace(mn, mx, bins + 1, endpoint=True) else: # adjust end points after binning bins = np.linspace(mn, mx, bins + 1, endpoint=True)