Skip to content

ENH: Add right argument to pandas.qcut to support left-inclusive binning #62938

@aallahyar

Description

@aallahyar

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

At the moment, it is not possible to perform left-inclusive binning using pandas.qcut.

For example, when q=2 the median value is always included in the first group:

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5])
print(pd.qcut(s, q=2).cat.categories)
# output: IntervalIndex([(0.999, 3.0], (3.0, 5.0]], dtype='interval[float64, right]')

It can be useful to have pandas.qcut produce left-inclusive bins, i.e., intervals of the form [left, right).

Feature Description

I propose to add a right argument pandas.qcut to allow left-inclusive binnig.
This will be very similar to pandas.cut which supports this argument.

Alternative Solutions

Of course, it is possible to get such a result by combinng quantile and pandas.cut, as follows:

import numpy as np
import pandas as pd

s = pd.Series([1, 2, 3, 4, 5])

quantiles = s.quantile(q=np.linspace(0, 1, num=3))
print(pd.cut(s, bins=quantiles, right=False).cat.categories)
# output: IntervalIndex([[1.0, 3.0), [3.0, 5.0)], dtype='interval[float64, left]')

But, considering that pandas.cut already supports this argument, it does make sense to include it in pandas.qcut as well.

Additional Context

I have already checked the pandas.qcut code and it seems that adding this argument requires a few lines of code.

If the devs agree, I would like to add this argument in a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions