Skip to content

Commit 6f3a721

Browse files
committed
BUG: pd.cut with bins=1 and input all 0s
The special case of running pd.cut() qith bins=1 an input containing all 0s raises a ValueError
1 parent c588dd1 commit 6f3a721

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,5 @@ Bug Fixes
587587
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
588588
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
589589
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
590+
591+
- Bug in ``pd.cut()`` (:issue:`15428`)

pandas/tests/tools/test_tile.py

+4
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ def test_single_bin(self):
297297
result = cut(s, 1, labels=False)
298298
tm.assert_series_equal(result, expected)
299299

300+
s = Series([0., 0.])
301+
result = cut(s, 1, labels=False)
302+
tm.assert_series_equal(result, expected)
303+
300304
def test_datetime_cut(self):
301305
# GH 14714
302306
# testing for time data to be present as series

pandas/tools/tile.py

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

106106
if mn == mx: # adjust end points before binning
107-
mn -= .001 * abs(mn)
108-
mx += .001 * abs(mx)
107+
mn -= .001 * abs(mn) if mn != 0 else .001
108+
mx += .001 * abs(mx) if mx != 0 else .001
109109
bins = np.linspace(mn, mx, bins + 1, endpoint=True)
110110
else: # adjust end points after binning
111111
bins = np.linspace(mn, mx, bins + 1, endpoint=True)

0 commit comments

Comments
 (0)