in core/maxframe/dataframe/reduction/core.py [0:0]
def _call_dataframe(self, df):
axis = getattr(self, "axis", None) or 0
level = getattr(self, "level", None)
skipna = getattr(self, "skipna", True)
numeric_only = getattr(self, "numeric_only", None)
bool_only = getattr(self, "bool_only", None)
self.axis = axis = validate_axis(axis, df)
func_name = getattr(self, "_func_name")
if level is not None and axis == 1:
raise NotImplementedError("Not support specify level for axis==1")
if func_name == "size":
reduced = pd.Series(
np.zeros(df.shape[1 - axis]),
index=df.dtypes.index if axis == 0 else None,
)
reduced_cols = list(reduced.index)
reduced_dtype = reduced.dtype
elif func_name == "custom_reduction":
empty_df = build_df(df, ensure_string=True)
reduced = getattr(self, "custom_reduction").__call_agg__(empty_df)
reduced_cols = list(reduced.index)
reduced_dtype = reduced.dtype
else:
reduced_cols, dtypes = [], []
for col, src_dt in df.dtypes.items():
dt = _get_df_reduction_dtype(
src_dt,
func_name,
axis=axis,
bool_only=bool_only,
skipna=skipna,
numeric_only=numeric_only,
)
if dt is not None:
reduced_cols.append(col)
dtypes.append(dt)
elif (
_level_reduction_keep_object
and numeric_only
and level is not None
and func_name in ("min", "max")
and src_dt == np.dtype(object)
): # pragma: no cover
reduced_cols.append(col)
dtypes.append(np.dtype(object))
if len(dtypes) == 0:
reduced_dtype = np.dtype("O")
elif all(dt == dtypes[0] for dt in dtypes):
reduced_dtype = dtypes[0]
else:
# as we already bypassed dtypes with same values,
# when has_mixed_bool is True, there are other dtypes
# other than bool.
has_mixed_bool = any(dt == np.dtype(bool) for dt in dtypes)
if _reduce_bool_as_object and has_mixed_bool:
reduced_dtype = np.dtype("O")
elif not all(isinstance(dt, np.dtype) for dt in dtypes):
# todo currently we return mixed dtypes as np.dtype('O').
# handle pandas Dtypes in the future more carefully.
reduced_dtype = np.dtype("O")
else:
reduced_dtype = np.find_common_type(dtypes, [])
if level is not None:
return self._call_groupby_level(df[reduced_cols], level)
if axis == 0:
reduced_shape = (len(reduced_cols),)
reduced_index_value = parse_index(pd.Index(reduced_cols), store_data=True)
else:
reduced_shape = (df.shape[0],)
reduced_index_value = parse_index(pd.RangeIndex(-1))
return self.new_series(
[df],
shape=reduced_shape,
dtype=reduced_dtype,
index_value=reduced_index_value,
)