Skip to content

Commit 1db8449

Browse files
committed
BUG: cut/qcut on Series with "retbins" (GH8589)
1 parent 7012d71 commit 1db8449

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

doc/source/whatsnew/v0.15.1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ Experimental
4747

4848
Bug Fixes
4949
~~~~~~~~~
50+
- Bug in ``cut``/``qcut`` when using ``Series`` and ``retbins=True`` (:issue:`8589`)

pandas/tools/tests/test_tile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ def test_qcut_return_categorical(self):
248248
ordered=True))
249249
tm.assert_series_equal(res, exp)
250250

251+
def test_series_retbins(self):
252+
# GH 8589
253+
s = Series(np.arange(4))
254+
result, bins = cut(s, 2, retbins=True)
255+
assert_equal(result.cat.codes.values, [0, 0, 1, 1])
256+
assert_almost_equal(bins, [-0.003, 1.5, 3])
257+
258+
result, bins = qcut(s, 2, retbins=True)
259+
assert_equal(result.cat.codes.values, [0, 0, 1, 1])
260+
assert_almost_equal(bins, [0, 1.5, 3])
251261

252262

253263
def curpath():

pandas/tools/tile.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
109109
if (np.diff(bins) < 0).any():
110110
raise ValueError('bins must increase monotonically.')
111111

112-
res = _bins_to_cuts(x, bins, right=right, labels=labels,retbins=retbins, precision=precision,
113-
include_lowest=include_lowest)
114-
if isinstance(x, Series):
115-
res = Series(res, index=x.index)
116-
return res
112+
return _bins_to_cuts(x, bins, right=right, labels=labels,retbins=retbins, precision=precision,
113+
include_lowest=include_lowest)
117114

118115

119116

@@ -168,18 +165,21 @@ def qcut(x, q, labels=None, retbins=False, precision=3):
168165
else:
169166
quantiles = q
170167
bins = algos.quantile(x, quantiles)
171-
res = _bins_to_cuts(x, bins, labels=labels, retbins=retbins,precision=precision,
172-
include_lowest=True)
173-
if isinstance(x, Series):
174-
res = Series(res, index=x.index)
175-
return res
168+
return _bins_to_cuts(x, bins, labels=labels, retbins=retbins,precision=precision,
169+
include_lowest=True)
176170

177171

178172

179173
def _bins_to_cuts(x, bins, right=True, labels=None, retbins=False,
180174
precision=3, name=None, include_lowest=False):
181-
if name is None and isinstance(x, Series):
182-
name = x.name
175+
x_is_series = isinstance(x, Series)
176+
series_index = None
177+
178+
if x_is_series:
179+
series_index = x.index
180+
if name is None:
181+
name = x.name
182+
183183
x = np.asarray(x)
184184

185185
side = 'left' if right else 'right'
@@ -224,6 +224,9 @@ def _bins_to_cuts(x, bins, right=True, labels=None, retbins=False,
224224
fac = fac.astype(np.float64)
225225
np.putmask(fac, na_mask, np.nan)
226226

227+
if x_is_series:
228+
fac = Series(fac, index=series_index)
229+
227230
if not retbins:
228231
return fac
229232

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy