Skip to content

Commit cb4c7ab

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 3f91d5a commit cb4c7ab

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
@@ -591,7 +591,7 @@ Bug Fixes
591591
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`, :issue:`15424`)
592592
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a numpy array (:issue:`15434`)
593593
- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)
594-
594+
- Bug in ``pd.cut()`` single bin on all 0s array raises ``ValueError`` (:issue:`15428`)
595595

596596
- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)
597597
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)

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)