Skip to content

Commit 55806cf

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 c52ff68 commit 55806cf

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ Bug Fixes
670670
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`, :issue:`15424`)
671671
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a numpy array (:issue:`15434`)
672672
- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)
673-
673+
- Bug in ``pd.cut()`` single bin on all 0s array raises ``ValueError`` (:issue:`15428`)
674674

675675
- Bug in ``SparseSeries.reindex`` on single level with list of length 1 (:issue:`15447`)
676676

pandas/tests/tools/test_tile.py

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

300+
# issue 15428
301+
s = Series([0., 0.])
302+
result = cut(s, 1, labels=False)
303+
tm.assert_series_equal(result, expected)
304+
300305
def test_datetime_cut(self):
301306
# GH 14714
302307
# 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)