@@ -29,7 +29,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
29
29
----------
30
30
x : array-like
31
31
Input array to be binned. It has to be 1-dimensional.
32
- bins : int or sequence of scalars
32
+ bins : int, sequence of scalars, or IntervalIndex
33
33
If `bins` is an int, it defines the number of equal-width bins in the
34
34
range of `x`. However, in this case, the range of `x` is extended
35
35
by .1% on each side to include the min or max values of `x`. If
@@ -78,10 +78,12 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
78
78
(6.533, 9.7], (0.191, 3.367]]
79
79
Categories (3, object): [(0.191, 3.367] < (3.367, 6.533] < (6.533, 9.7]],
80
80
array([ 0.1905 , 3.36666667, 6.53333333, 9.7 ]))
81
+
81
82
>>> pd.cut(np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1]), 3,
82
83
labels=["good","medium","bad"])
83
84
[good, good, good, medium, bad, good]
84
85
Categories (3, object): [good < medium < bad]
86
+
85
87
>>> pd.cut(np.ones(5), 4, labels=False)
86
88
array([1, 1, 1, 1, 1], dtype=int64)
87
89
"""
@@ -119,6 +121,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
119
121
else :
120
122
bins [- 1 ] += adj
121
123
124
+ elif isinstance (bins , IntervalIndex ):
125
+ pass
122
126
else :
123
127
bins = np .asarray (bins )
124
128
bins = _convert_bin_to_numeric_type (bins , dtype )
@@ -179,9 +183,11 @@ def qcut(x, q, labels=None, retbins=False, precision=3, duplicates='raise'):
179
183
>>> pd.qcut(range(5), 4)
180
184
[[0, 1], [0, 1], (1, 2], (2, 3], (3, 4]]
181
185
Categories (4, object): [[0, 1] < (1, 2] < (2, 3] < (3, 4]]
186
+
182
187
>>> pd.qcut(range(5), 3, labels=["good","medium","bad"])
183
188
[good, good, medium, bad, bad]
184
189
Categories (3, object): [good < medium < bad]
190
+
185
191
>>> pd.qcut(range(5), 4, labels=False)
186
192
array([0, 0, 1, 2, 3], dtype=int64)
187
193
"""
@@ -210,6 +216,13 @@ def _bins_to_cuts(x, bins, right=True, labels=None,
210
216
raise ValueError ("invalid value for 'duplicates' parameter, "
211
217
"valid options are: raise, drop" )
212
218
219
+ if isinstance (bins , IntervalIndex ):
220
+ # we have a fast-path here
221
+ ids = bins .get_indexer (x )
222
+ result = algos .take_nd (bins , ids )
223
+ result = Categorical (result , ordered = True )
224
+ return result , bins
225
+
213
226
unique_bins = algos .unique (bins )
214
227
if len (unique_bins ) < len (bins ) and len (bins ) != 2 :
215
228
if duplicates == 'raise' :
0 commit comments