Skip to content

Commit d842b70

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 d842b70

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
@@ -499,7 +499,7 @@ Bug Fixes
499499
- Bug in ``pd.read_csv()`` in which a file containing a row with many columns followed by rows with fewer columns would cause a crash (:issue:`14125`)
500500
- Bug in ``pd.tools.hashing.hash_pandas_object()`` in which hashing of categoricals depended on the ordering of categories, instead of just their values. (:issue:`15143`)
501501
- Bug in ``.groupby(..).resample()`` when passed the ``on=`` kwarg. (:issue:`15021`)
502-
502+
- Bug in ``pd.cut()`` single bin on all 0s array raises ``ValueError`` (:issue:`15428`)
503503
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`)
504504

505505

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)