code: def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
          include_lowest=False, duplicates='raise'):
      x_is_series, series_index, name, x = _preprocess_for_cut(x)
      x, dtype = _coerce_to_type(x)
      if not np.iterable(bins):
          if is_scalar(bins) and bins < 1:
              raise ValueError("`bins` should be a positive integer.")
          try:
              sz = x.size
          except AttributeError:
              x = np.asarray(x)
              sz = x.size
          if sz == 0:
              raise ValueError('Cannot cut empty array')
          rng = (nanops.nanmin(x), nanops.nanmax(x))
          mn, mx = [mi + 0.0 for mi in rng]
          if np.isinf(mn) or np.isinf(mx):
              raise ValueError('cannot specify integer `bins` when input data '
                               'contains infinity')
          elif mn == mx:
              mn -= .001 * abs(mn) if mn != 0 else .001
              mx += .001 * abs(mx) if mx != 0 else .001
              bins = np.linspace(mn, mx, bins + 1, endpoint=True)
          else:
              bins = np.linspace(mn, mx, bins + 1, endpoint=True)
              adj = (mx - mn) * 0.001
              if right:
                  bins[0] -= adj
              else:
                  bins[-1] += adj
      elif isinstance(bins, IntervalIndex):
          if bins.is_overlapping:
              raise ValueError('Overlapping IntervalIndex is not accepted.')
      else:
          if is_datetime64tz_dtype(bins):
              bins = np.asarray(bins, dtype=_NS_DTYPE)
          else:
              bins = np.asarray(bins)
          bins = _convert_bin_to_numeric_type(bins, dtype)
          if (np.diff(bins.astype('float64')) < 0).any():
              raise ValueError('bins must increase monotonically.')
      fac, bins = _bins_to_cuts(x, bins, right=right, labels=labels,
                                precision=precision,
                                include_lowest=include_lowest,
                                dtype=dtype,
                                duplicates=duplicates)
      return _postprocess_for_cut(fac, bins, retbins, x_is_series,
                                  series_index, name, dtype)