in core/maxframe/dataframe/misc/cut.py [0:0]
def __call__(self, x):
if isinstance(x, pd.Series):
x = asseries(x)
elif not isinstance(x, ENTITY_TYPE):
x = astensor(x)
if x.ndim != 1:
raise ValueError("Input array must be 1 dimensional")
if x.size == 0:
raise ValueError("Cannot cut empty array")
inputs = [x]
if self.labels is not None and not isinstance(self.labels, (bool, ENTITY_TYPE)):
self.labels = np.asarray(self.labels)
# infer dtype
x_empty = (
pd.Series([1], dtype=x.dtype)
if isinstance(x, SERIES_TYPE)
else np.asarray([1], dtype=x.dtype)
)
if isinstance(self.bins, INDEX_TYPE):
bins = self.bins.index_value.to_pandas()
inputs.append(self.bins)
bins_unknown = True
elif isinstance(self.bins, ENTITY_TYPE):
bins = np.asarray([2], dtype=self.bins.dtype)
inputs.append(self.bins)
bins_unknown = True
else:
bins = self.bins
bins_unknown = isinstance(self.bins, Integral)
if isinstance(self.labels, ENTITY_TYPE):
bins_unknown = True
labels = None
inputs.append(self.labels)
else:
if self.labels is False or not bins_unknown:
labels = self.labels
else:
labels = None
ret = pd.cut(
x_empty,
bins,
right=self.right,
labels=labels,
retbins=True,
include_lowest=self.include_lowest,
duplicates=self.duplicates,
)
kws = []
output_types = []
if bins_unknown and isinstance(ret[0].dtype, pd.CategoricalDtype):
# inaccurate dtype, just create an empty one
out_dtype = pd.CategoricalDtype()
else:
out_dtype = ret[0].dtype
if isinstance(ret[0], pd.Series):
output_types.append(OutputType.series)
kws.append(
{
"dtype": out_dtype,
"shape": x.shape,
"index_value": x.index_value,
"name": x.name,
}
)
elif isinstance(ret[0], np.ndarray):
output_types.append(OutputType.tensor)
kws.append(
{"dtype": out_dtype, "shape": x.shape, "order": TensorOrder.C_ORDER}
)
else:
assert isinstance(ret[0], pd.Categorical)
output_types.append(OutputType.categorical)
kws.append(
{
"dtype": out_dtype,
"shape": x.shape,
"categories_value": parse_index(
out_dtype.categories, store_data=True
),
}
)
if self.retbins:
if isinstance(self.bins, (pd.IntervalIndex, INDEX_TYPE)):
output_types.append(OutputType.index)
kws.append(
{
"dtype": self.bins.dtype,
"shape": self.bins.shape,
"index_value": (
self.bins.index_value
if isinstance(self.bins, INDEX_TYPE)
else parse_index(self.bins, store_data=False)
),
"name": self.bins.name,
}
)
else:
output_types.append(OutputType.tensor)
kws.append(
{
"dtype": ret[1].dtype,
"shape": ret[1].shape if ret[1].size > 0 else (np.nan,),
"order": TensorOrder.C_ORDER,
}
)
self.output_types = output_types
return ExecutableTuple(self.new_tileables(inputs, kws=kws))