# Buggy code
def preprocess(cls, op, in_data=None):
    in_data = in_data or op.inputs[0]
    axis_shape = in_data.shape[op.axis]
    axis_chunk_shape = in_data.chunk_shape[op.axis]

    # rechunk to ensure all chunks on axis have rough same size
    axis_chunk_shape = min(axis_chunk_shape, int(np.sqrt(axis_shape)))
    if np.isnan(axis_shape) or any(np.isnan(s) for s in in_data.nsplits[op.axis]):
        raise TilesError(
            "fail to tile because either the shape of "
            "input data on axis {} has unknown shape or chunk shape".format(op.axis)
        )
    chunk_size = int(axis_shape / axis_chunk_shape)
    chunk_sizes = [chunk_size for _ in range(int(axis_shape // chunk_size))]
    if axis_shape % chunk_size > 0:
        chunk_sizes[-1] += axis_shape % chunk_size
    in_data = in_data.rechunk({op.axis: tuple(chunk_sizes)})._inplace_tile()
    axis_chunk_shape = in_data.chunk_shape[op.axis]

    left_chunk_shape = (
        in_data.chunk_shape[: op.axis] + in_data.chunk_shape[op.axis + 1 :]
    )
    if len(left_chunk_shape) > 0:
        out_idxes = itertools.product(*(range(s) for s in left_chunk_shape))
    else:
        out_idxes = [()]
    # if the size except axis has more than 1, the sorted values on each one may be different
    # another shuffle would be required to make sure each axis except to sort
    # has elements with identical size
    extra_shape = [s for i, s in enumerate(in_data.shape) if i != op.axis]
    if getattr(op, "need_align", None) is None:
        need_align = bool(np.prod(extra_shape, dtype=int) != 1)
    else:
        need_align = op.need_align

    return in_data, axis_chunk_shape, out_idxes, need_align


# Traceback (AttributeError)
In [25]: merge_df = parsing_df.append(pFold_4_df)

In [26]: mDf_g = merge_df.groupby(["query","template"])

In [27]: mDf_g.execute()
Out[27]: ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/anaconda3/envs/py37/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
700                 type_pprinters=self.type_printers,
701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
703             printer.flush()
704             return stream.getvalue()

~/anaconda3/envs/py37/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
392                         if cls is not object \
393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
395
396             return _default_pprint(obj, self, cycle)

~/anaconda3/envs/py37/lib/python3.7/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
698     """A pprint that just redirects to the normal repr function."""
699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
701     lines = output.splitlines()
702     with p.group():

~/anaconda3/envs/py37/lib/python3.7/site-packages/mars/dataframe/core.py in __repr__(self)
998
999     def __repr__(self):
-> 1000         return self._to_str(representation=True)
1001
1002     def _repr_html_(self):

~/anaconda3/envs/py37/lib/python3.7/site-packages/mars/dataframe/core.py in _to_str(self, representation)
968         else:
969             corner_data = fetch_corner_data(
--> 970                 self, session=self._executed_sessions[-1])
971
972             buf = StringIO()

~/anaconda3/envs/py37/lib/python3.7/site-packages/mars/dataframe/utils.py in fetch_corner_data(df_or_series, session)
768         return df_or_series.fetch(session=session)
769     else:
--> 770         head = iloc(df_or_series)[:index_size]
771         tail = iloc(df_or_series)[-index_size:]
772         head_data, tail_data = \

~/anaconda3/envs/py37/lib/python3.7/site-packages/mars/dataframe/indexing/iloc.py in __getitem__(self, indexes)
111         else:
112             op = SeriesIlocGetItem(indexes=process_iloc_indexes(self._obj, indexes))
--> 113         return op(self._obj)
114
115     def __setitem__(self, indexes, value):

~/anaconda3/envs/py37/lib/python3.7/site-packages/mars/dataframe/indexing/iloc.py in __call__(self, series)
485             index_value = indexing_index_value(series.index_value, self.indexes[0])
486             inputs = [series] + [index for index in self._indexes if isinstance(index, (Base, Entity))]
--> 487             return self.new_series(inputs, shape=shape, dtype=series.dtype,
488                                    index_value=index_value, name=series.name)
489

AttributeError: 'DataFrameGroupByData' object has no attribute 'dtype'

# Fixed code
def preprocess(cls, op, in_data=None):
    if in_data is None:
        in_data = op.inputs[0]
    axis_shape = in_data.shape[op.axis]
    axis_chunk_shape = in_data.chunk_shape[op.axis]

    # rechunk to ensure all chunks on axis have rough same size
    has_unknown_shape = False
    for ns in in_data.nsplits:
        if any(np.isnan(s) for s in ns):
            has_unknown_shape = True
            break

    if not has_unknown_shape:
        axis_chunk_shape = min(axis_chunk_shape, int(np.sqrt(axis_shape)))
        if np.isnan(axis_shape) or any(np.isnan(s) for s in in_data.nsplits[op.axis]):
            raise TilesError(
                "fail to tile because either the shape of "
                "input data on axis {} has unknown shape or chunk shape".format(op.axis)
            )
        chunk_size = int(axis_shape / axis_chunk_shape)
        chunk_sizes = [chunk_size for _ in range(int(axis_shape // chunk_size))]
        if axis_shape % chunk_size > 0:
            chunk_sizes[-1] += axis_shape % chunk_size
        in_data = in_data.rechunk({op.axis: tuple(chunk_sizes)})._inplace_tile()
        axis_chunk_shape = in_data.chunk_shape[op.axis]

    left_chunk_shape = (
        in_data.chunk_shape[: op.axis] + in_data.chunk_shape[op.axis + 1 :]
    )
    if len(left_chunk_shape) > 0:
        out_idxes = itertools.product(*(range(s) for s in left_chunk_shape))
    else:
        out_idxes = [()]
    # if the size except axis has more than 1, the sorted values on each one may be different
    # another shuffle would be required to make sure each axis except to sort
    # has elements with identical size
    extra_shape = [s for i, s in enumerate(in_data.shape) if i != op.axis]
    if getattr(op, "need_align", None) is None:
        need_align = bool(np.prod(extra_shape, dtype=int) != 1)
    else:
        need_align = op.need_align

    return in_data, axis_chunk_shape, out_idxes, need_align