Skip to content

ENH: add "right" argument to "qcut" #58986

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def qcut(
retbins: bool = False,
precision: int = 3,
duplicates: str = "raise",
right: bool = True,
):
"""
Quantile-based discretization function.
Expand All @@ -304,6 +305,8 @@ def qcut(
The precision at which to store and display the bins labels.
duplicates : {default 'raise', 'drop'}, optional
If bin edges are not unique, raise ValueError or drop non-uniques.
right : bool, default True
Indicates whether bins includes the rightmost edge or not.

Returns
-------
Expand Down Expand Up @@ -346,13 +349,29 @@ def qcut(

bins = x_idx.to_series().dropna().quantile(quantiles)

if isinstance(bins[0], (np.datetime64, Timestamp)):
time_delta = np.timedelta64(1, 's')
bins = np.array(bins, dtype='datetime64[s]')
if not right:
bins[-1] = bins[-1] + 4 * time_delta
else:
bins[1:] = bins[1:] + 4 * time_delta
else:
eps = np.finfo(bins.dtype).eps
bins = np.array(bins)
if not right:
bins[-1] = bins[-1] + 4 * eps
else:
bins[1:] = bins[1:] + 4 * eps

fac, bins = _bins_to_cuts(
x_idx,
Index(bins),
labels=labels,
precision=precision,
include_lowest=True,
duplicates=duplicates,
right=right,
)

return _postprocess_for_cut(fac, bins, retbins, original)
Expand Down