Skip to content

BUG: cut/qcut on Series with "retbins" (GH8589) #8622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.15.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ Experimental

Bug Fixes
~~~~~~~~~
- Bug in ``cut``/``qcut`` when using ``Series`` and ``retbins=True`` (:issue:`8589`)
10 changes: 10 additions & 0 deletions pandas/tools/tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ def test_qcut_return_categorical(self):
ordered=True))
tm.assert_series_equal(res, exp)

def test_series_retbins(self):
# GH 8589
s = Series(np.arange(4))
result, bins = cut(s, 2, retbins=True)
assert_equal(result.cat.codes.values, [0, 0, 1, 1])
assert_almost_equal(bins, [-0.003, 1.5, 3])

result, bins = qcut(s, 2, retbins=True)
assert_equal(result.cat.codes.values, [0, 0, 1, 1])
assert_almost_equal(bins, [0, 1.5, 3])


def curpath():
Expand Down
27 changes: 15 additions & 12 deletions pandas/tools/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
if (np.diff(bins) < 0).any():
raise ValueError('bins must increase monotonically.')

res = _bins_to_cuts(x, bins, right=right, labels=labels,retbins=retbins, precision=precision,
include_lowest=include_lowest)
if isinstance(x, Series):
res = Series(res, index=x.index)
return res
return _bins_to_cuts(x, bins, right=right, labels=labels,retbins=retbins, precision=precision,
include_lowest=include_lowest)



Expand Down Expand Up @@ -168,18 +165,21 @@ def qcut(x, q, labels=None, retbins=False, precision=3):
else:
quantiles = q
bins = algos.quantile(x, quantiles)
res = _bins_to_cuts(x, bins, labels=labels, retbins=retbins,precision=precision,
include_lowest=True)
if isinstance(x, Series):
res = Series(res, index=x.index)
return res
return _bins_to_cuts(x, bins, labels=labels, retbins=retbins,precision=precision,
include_lowest=True)



def _bins_to_cuts(x, bins, right=True, labels=None, retbins=False,
precision=3, name=None, include_lowest=False):
if name is None and isinstance(x, Series):
name = x.name
x_is_series = isinstance(x, Series)
series_index = None

if x_is_series:
series_index = x.index
if name is None:
name = x.name

x = np.asarray(x)

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

if x_is_series:
fac = Series(fac, index=series_index)

if not retbins:
return fac

Expand Down