mars.dataframe.cut¶

mars.dataframe.
cut
(x, bins, right: bool = True, labels=None, retbins: bool = False, precision: int = 3, include_lowest: bool = False, duplicates: str = 'raise')[source]¶ Bin values into discrete intervals.
Use cut when you need to segment and sort data values into bins. This function is also useful for going from a continuous variable to a categorical variable. For example, cut could convert ages to groups of age ranges. Supports binning into an equal number of bins, or a prespecified array of bins.
 Parameters
x (arraylike) – The input array to be binned. Must be 1dimensional.
bins (int, sequence of scalars, or IntervalIndex) –
The criteria to bin by.
int : Defines the number of equalwidth bins in the range of x. The range of x is extended by .1% on each side to include the minimum and maximum values of x.
sequence of scalars : Defines the bin edges allowing for nonuniform width. No extension of the range of x is done.
IntervalIndex : Defines the exact bins to be used. Note that IntervalIndex for bins must be nonoverlapping.
right (bool, default True) – Indicates whether bins includes the rightmost edge or not. If
right == True
(the default), then the bins[1, 2, 3, 4]
indicate (1,2], (2,3], (3,4]. This argument is ignored when bins is an IntervalIndex.labels (array or False, default None) – Specifies the labels for the returned bins. Must be the same length as the resulting bins. If False, returns only integer indicators of the bins. This affects the type of the output container (see below). This argument is ignored when bins is an IntervalIndex. If True, raises an error.
retbins (bool, default False) – Whether to return the bins or not. Useful when bins is provided as a scalar.
precision (int, default 3) – The precision at which to store and display the bins labels.
include_lowest (bool, default False) – Whether the first interval should be leftinclusive or not.
duplicates ({default 'raise', 'drop'}, optional) – If bin edges are not unique, raise ValueError or drop nonuniques.
 Returns
out (Categorical, Series, or Tensor) – An arraylike object representing the respective bin for each value of x. The type depends on the value of labels.
True (default) : returns a Series for Series x or a Categorical for all other inputs. The values stored within are Interval dtype.
sequence of scalars : returns a Series for Series x or a Categorical for all other inputs. The values stored within are whatever the type in the sequence is.
False : returns a tensor of integers.
bins (Tensor or IntervalIndex.) – The computed or specified bins. Only returned when retbins=True. For scalar or sequence bins, this is a tensor with the computed bins. If set duplicates=drop, bins will drop nonunique bin. For an IntervalIndex bins, this is equal to bins.
See also
qcut
Discretize variable into equalsized buckets based on rank or based on sample quantiles.
Categorical
Array type for storing data that come from a fixed set of values.
Series
Onedimensional array with axis labels (including time series).
IntervalIndex
Immutable Index implementing an ordered, sliceable set.
Notes
Any NA values will be NA in the result. Out of bounds values will be NA in the resulting Series or Categorical object.
Examples
Discretize into three equalsized bins.
>>> import mars.tensor as mt >>> import mars.dataframe as md
>>> md.cut(mt.array([1, 7, 5, 4, 6, 3]), 3).execute() ... [(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ... Categories (3, interval[float64]): [(0.994, 3.0] < (3.0, 5.0] ...
>>> md.cut(mt.array([1, 7, 5, 4, 6, 3]), 3, retbins=True).execute() ... ([(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ... Categories (3, interval[float64]): [(0.994, 3.0] < (3.0, 5.0] ... array([0.994, 3. , 5. , 7. ]))
Discovers the same bins, but assign them specific labels. Notice that the returned Categorical’s categories are labels and is ordered.
>>> md.cut(mt.array([1, 7, 5, 4, 6, 3]), ... 3, labels=["bad", "medium", "good"]).execute() [bad, good, medium, medium, good, bad] Categories (3, object): [bad < medium < good]
labels=False
implies you just want the bins back.>>> md.cut([0, 1, 1, 2], bins=4, labels=False).execute() array([0, 1, 1, 3])
Passing a Series as an input returns a Series with categorical dtype:
>>> s = md.Series(mt.array([2, 4, 6, 8, 10]), ... index=['a', 'b', 'c', 'd', 'e']) >>> md.cut(s, 3).execute() ... a (1.992, 4.667] b (1.992, 4.667] c (4.667, 7.333] d (7.333, 10.0] e (7.333, 10.0] dtype: category Categories (3, interval[float64]): [(1.992, 4.667] < (4.667, ...
Passing a Series as an input returns a Series with mapping value. It is used to map numerically to intervals based on bins.
>>> s = md.Series(mt.array([2, 4, 6, 8, 10]), ... index=['a', 'b', 'c', 'd', 'e']) >>> md.cut(s, [0, 2, 4, 6, 8, 10], labels=False, retbins=True, right=False).execute() ... (a 0.0 b 1.0 c 2.0 d 3.0 e NaN dtype: float64, array([0, 2, 4, 6, 8, 10]))
Use drop optional when bins is not unique
>>> md.cut(s, [0, 2, 4, 6, 10, 10], labels=False, retbins=True, ... right=False, duplicates='drop').execute() ... (a 0.0 b 1.0 c 2.0 d 3.0 e NaN dtype: float64, array([0, 2, 4, 6, 10]))
Passing an IntervalIndex for bins results in those categories exactly. Notice that values not covered by the IntervalIndex are set to NaN. 0 is to the left of the first bin (which is closed on the right), and 1.5 falls between two bins.
>>> bins = md.Index(pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)])) >>> md.cut([0, 0.5, 1.5, 2.5, 4.5], bins).execute() [NaN, (0, 1], NaN, (2, 3], (4, 5]] Categories (3, interval[int64]): [(0, 1] < (2, 3] < (4, 5]]