Skip to content

Commit 2fc0c68

Browse files
luca-sjreback
authored andcommitted
BUG: pandas.cut and negative values #14652
closes #14652 Author: Luca Scarabello <[email protected]> Author: Luca <[email protected]> Closes #14663 from luca-s/issue_14652 and squashes the following commits: 8db26db [Luca Scarabello] Moved new test location to pandas\tools\tests\test_tile.py 90dd07d [Luca Scarabello] Updated whatsnew d6b3da8 [Luca Scarabello] fixed flake8 compliance fdc55b9 [Luca Scarabello] Added test case for #14652 d5790e2 [Luca Scarabello] updated whatsnew v0.19.2 2db0c7a [Luca] BUG: pandas.cut and negative values #14652
1 parent 726efc7 commit 2fc0c68

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

doc/source/whatsnew/v0.19.2.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ Performance Improvements
2323
Bug Fixes
2424
~~~~~~~~~
2525

26-
- compat with ``dateutil==2.6.0`` for testing (:issue:`14621`)
27-
- allow ``nanoseconds`` in ``Timestamp.replace`` kwargs (:issue:`14621`)
26+
- Compat with ``dateutil==2.6.0``; segfault reported in the testing suite (:issue:`14621`)
27+
- Allow ``nanoseconds`` in ``Timestamp.replace`` as a kwarg (:issue:`14621`)
28+
- Bug in ``pd.cut`` with negative values and a single bin (:issue:`14652`)

pandas/tools/tests/test_tile.py

+12
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ def test_series_retbins(self):
271271
np.array([0, 0, 1, 1], dtype=np.int8))
272272
tm.assert_numpy_array_equal(bins, np.array([0, 1.5, 3]))
273273

274+
def test_single_bin(self):
275+
# issue 14652
276+
expected = Series([0, 0])
277+
278+
s = Series([9., 9.])
279+
result = cut(s, 1, labels=False)
280+
tm.assert_series_equal(result, expected)
281+
282+
s = Series([-9., -9.])
283+
result = cut(s, 1, labels=False)
284+
tm.assert_series_equal(result, expected)
285+
274286

275287
def curpath():
276288
pth, _ = os.path.split(os.path.abspath(__file__))

pandas/tools/tile.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
9898
mn, mx = [mi + 0.0 for mi in rng]
9999

100100
if mn == mx: # adjust end points before binning
101-
mn -= .001 * mn
102-
mx += .001 * mx
101+
mn -= .001 * abs(mn)
102+
mx += .001 * abs(mx)
103103
bins = np.linspace(mn, mx, bins + 1, endpoint=True)
104104
else: # adjust end points after binning
105105
bins = np.linspace(mn, mx, bins + 1, endpoint=True)

0 commit comments

Comments
 (0)