# Buggy code
def tile(cls, op):
    in_groupby = op.inputs[0]
    out_df = op.outputs[0]

    chunks = []
    for c in in_groupby.chunks:
        new_op = op.copy().reset_key()

        new_index = parse_index(pd.RangeIndex(-1), c.key)
        if op.output_types[0] == OutputType.dataframe:
            chunks.append(
                new_op.new_chunk(
                    [c],
                    index=c.index,
                    shape=(np.nan, len(out_df.dtypes)),
                    dtypes=out_df.dtypes,
                    columns_value=out_df.columns_value,
                    index_value=new_index,
                )
            )
        else:
            chunks.append(
                new_op.new_chunk(
                    [c],
                    index=(c.index[0],),
                    shape=(np.nan,),
                    dtype=out_df.dtype,
                    index_value=new_index,
                )
            )

    new_op = op.copy().reset_key()
    kw = out_df.params.copy()
    kw["chunks"] = chunks
    if op.output_types[0] == OutputType.dataframe:
        kw["nsplits"] = ((np.nan,) * len(chunks), (len(out_df.dtypes),))
    else:
        kw["nsplits"] = ((np.nan,) * len(chunks),)
    return new_op.new_tileables([in_groupby], **kw)


# Traceback (KeyError)
Attempt 1: Unexpected error KeyError occurred in executing operand bc8dccd428eb0f6261420866f7206b73 in 0.0.0.0:23252
Traceback (most recent call last):
File "/Users/wenjun.swj/Code/mars/mars/promise.py", line 378, in _wrapped
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 383, in _wrapped
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/worker/execution.py", line 501, in execute_graph
graph_record = self._graph_records[(session_id, graph_key)] = GraphExecutionRecord(
File "/Users/wenjun.swj/Code/mars/mars/worker/execution.py", line 57, in __init__
graph = self.graph = deserialize_graph(graph_serialized)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 317, in deserialize_graph
return graph_cls.from_json(json_obj)
File "mars/graph.pyx", line 440, in mars.graph.DirectedGraph.from_json
return cls.deserialize(SerializableGraph.from_json(json_obj))
File "mars/serialize/core.pyx", line 718, in mars.serialize.core.Serializable.from_json
return cls.deserialize(provider, obj)
File "mars/serialize/core.pyx", line 689, in mars.serialize.core.Serializable.deserialize
[cb(key_to_instance) for cb in callbacks]
File "mars/serialize/jsonserializer.pyx", line 787, in mars.serialize.jsonserializer.JsonSerializeProvider.deserialize_field.cb
o = subs[val.key, val.id]
KeyError: ('b6888c78a929d77f42d7f3953fc813d9', '140581826655232')

# Fixed code
def tile(cls, op):
    in_groupby = op.inputs[0]
    out_df = op.outputs[0]

    chunks = []
    for c in in_groupby.chunks:
        new_op = op.copy().reset_key()

        new_index = parse_index(pd.RangeIndex(-1), c.key)
        if op.output_types[0] == OutputType.dataframe:
            chunks.append(
                new_op.new_chunk(
                    [c],
                    index=c.index,
                    shape=(np.nan, len(out_df.dtypes)),
                    dtypes=out_df.dtypes,
                    columns_value=out_df.columns_value,
                    index_value=new_index,
                )
            )
        else:
            chunks.append(
                new_op.new_chunk(
                    [c],
                    index=(c.index[0],),
                    shape=(np.nan,),
                    dtype=out_df.dtype,
                    index_value=new_index,
                    name=out_df.name,
                )
            )

    new_op = op.copy().reset_key()
    kw = out_df.params.copy()
    kw["chunks"] = chunks
    if op.output_types[0] == OutputType.dataframe:
        kw["nsplits"] = ((np.nan,) * len(chunks), (len(out_df.dtypes),))
    else:
        kw["nsplits"] = ((np.nan,) * len(chunks),)
    return new_op.new_tileables([in_groupby], **kw)