From 2db0c7a2d8eead67a05cf9c8d6c24d608d4d52c9 Mon Sep 17 00:00:00 2001 From: Luca Date: Tue, 15 Nov 2016 20:59:07 +0100 Subject: [PATCH 1/6] BUG: pandas.cut and negative values #14652 Fix issue #14652 --- pandas/tools/tile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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) From d5790e2418b81a3973bb811ab3675b06784dbd1b Mon Sep 17 00:00:00 2001 From: Luca Scarabello Date: Wed, 16 Nov 2016 15:50:21 +0100 Subject: [PATCH 2/6] updated whatsnew v0.19.2 --- doc/source/whatsnew/v0.19.2.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index dc11dd17bfdd7..f5568cf3462a2 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`) +- fix ``pandas.cut`` with one negative value and one bin (:issue:`14652`) From fdc55b98213f0f09dcfe83444838500ff8a144c9 Mon Sep 17 00:00:00 2001 From: Luca Scarabello Date: Wed, 16 Nov 2016 16:21:28 +0100 Subject: [PATCH 3/6] Added test case for #14652 --- pandas/tests/test_base.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index eaa316bfd8157..3ec8a15abd396 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -1095,6 +1095,20 @@ def f(): self.assertRaises(AttributeError, f) self.assertFalse(hasattr(t, "b")) +class TestTile(tm.TestCase): + + def test_single_bin(self): + + expected = pd.Series([0, 0]) + + s = pd.Series([9., 9.]) + result = pd.cut(s, 1, labels=False) + tm.assert_series_equal(result, expected) + + s = pd.Series([-9., -9.]) + result = pd.cut(s, 1, labels=False) + tm.assert_series_equal(result, expected) + if __name__ == '__main__': import nose From d6b3da83ced2ec1c5eb4b5c01aa999f61ce6f951 Mon Sep 17 00:00:00 2001 From: Luca Scarabello Date: Wed, 16 Nov 2016 18:06:15 +0100 Subject: [PATCH 4/6] fixed flake8 compliance --- pandas/tests/test_base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index 3ec8a15abd396..d11084cb6429c 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -1095,6 +1095,7 @@ def f(): self.assertRaises(AttributeError, f) self.assertFalse(hasattr(t, "b")) + class TestTile(tm.TestCase): def test_single_bin(self): From 90dd07d97c5e715b8d85b2a89c1f0bf5038bd771 Mon Sep 17 00:00:00 2001 From: Luca Scarabello Date: Thu, 17 Nov 2016 09:36:09 +0100 Subject: [PATCH 5/6] Updated whatsnew --- doc/source/whatsnew/v0.19.2.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index f5568cf3462a2..92fd8fbdc3b4d 100644 --- a/doc/source/whatsnew/v0.19.2.txt +++ b/doc/source/whatsnew/v0.19.2.txt @@ -25,4 +25,4 @@ Bug Fixes - compat with ``dateutil==2.6.0`` for testing (:issue:`14621`) - allow ``nanoseconds`` in ``Timestamp.replace`` kwargs (:issue:`14621`) -- fix ``pandas.cut`` with one negative value and one bin (:issue:`14652`) +- Bug in ``pd.cut`` (:issue:`14652`) From 8db26db02509300c04150d69e1e1c779cab8cdb6 Mon Sep 17 00:00:00 2001 From: Luca Scarabello Date: Thu, 17 Nov 2016 09:42:03 +0100 Subject: [PATCH 6/6] Moved new test location to pandas\tools\tests\test_tile.py --- pandas/tests/test_base.py | 15 --------------- pandas/tools/tests/test_tile.py | 12 ++++++++++++ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index d11084cb6429c..eaa316bfd8157 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -1096,21 +1096,6 @@ def f(): self.assertFalse(hasattr(t, "b")) -class TestTile(tm.TestCase): - - def test_single_bin(self): - - expected = pd.Series([0, 0]) - - s = pd.Series([9., 9.]) - result = pd.cut(s, 1, labels=False) - tm.assert_series_equal(result, expected) - - s = pd.Series([-9., -9.]) - result = pd.cut(s, 1, labels=False) - tm.assert_series_equal(result, expected) - - if __name__ == '__main__': import nose 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__))