# Buggy code
def execute(cls, ctx, op):
    def _base_concat(chunk, inputs):
        # auto generated concat when executing a DataFrame, Series or Index
        if chunk.op.output_types[0] == OutputType.dataframe:
            return _auto_concat_dataframe_chunks(chunk, inputs)
        elif chunk.op.output_types[0] == OutputType.series:
            return _auto_concat_series_chunks(chunk, inputs)
        elif chunk.op.output_types[0] == OutputType.index:
            return _auto_concat_index_chunks(chunk, inputs)
        elif chunk.op.output_types[0] == OutputType.categorical:
            return _auto_concat_categorical_chunks(chunk, inputs)
        else:  # pragma: no cover
            raise TypeError(
                "Only DataFrameChunk, SeriesChunk, IndexChunk, "
                "and CategoricalChunk can be automatically concatenated"
            )

    def _auto_concat_dataframe_chunks(chunk, inputs):
        xdf = pd if isinstance(inputs[0], (pd.DataFrame, pd.Series)) else cudf

        if chunk.op.axis is not None:
            return xdf.concat(inputs, axis=op.axis)

        # auto generated concat when executing a DataFrame
        if len(inputs) == 1:
            ret = inputs[0]
        else:
            max_rows = max(inp.index[0] for inp in chunk.inputs)
            min_rows = min(inp.index[0] for inp in chunk.inputs)
            n_rows = max_rows - min_rows + 1
            n_cols = int(len(inputs) // n_rows)
            assert n_rows * n_cols == len(inputs)

            concats = []
            for i in range(n_rows):
                if n_cols == 1:
                    concats.append(inputs[i])
                else:
                    concat = xdf.concat(
                        [inputs[i * n_cols + j] for j in range(n_cols)], axis=1
                    )
                    concats.append(concat)

            if xdf is pd:
                # The `sort=False` is to suppress a `FutureWarning` of pandas,
                # when the index or column of chunks to concatenate is not aligned,
                # which may happens for certain ops.
                #
                # See also Note [Columns of Left Join] in test_merge_execution.py.
                ret = xdf.concat(concats, sort=False)
            else:
                ret = xdf.concat(concats)
                # cuDF will lost index name when concat two seriess.
                ret.index.name = concats[0].index.name

        if getattr(chunk.index_value, "should_be_monotonic", False):
            ret.sort_index(inplace=True)
        if getattr(chunk.columns_value, "should_be_monotonic", False):
            ret.sort_index(axis=1, inplace=True)
        return ret

    def _auto_concat_series_chunks(chunk, inputs):
        # auto generated concat when executing a Series
        if all(np.isscalar(inp) for inp in inputs):
            return pd.Series(inputs)
        else:
            if len(inputs) == 1:
                concat = inputs[0]
            else:
                xdf = pd if isinstance(inputs[0], pd.Series) else cudf
                if chunk.op.axis is not None:
                    concat = xdf.concat(inputs, axis=chunk.op.axis)
                else:
                    concat = xdf.concat(inputs)
            if getattr(chunk.index_value, "should_be_monotonic", False):
                concat.sort_index(inplace=True)
            return concat

    def _auto_concat_index_chunks(chunk, inputs):
        if len(inputs) == 1:
            xdf = pd if isinstance(inputs[0], pd.Index) else cudf
            concat_df = xdf.DataFrame(index=inputs[0])
        else:
            xdf = pd if isinstance(inputs[0], pd.Index) else cudf
            empty_dfs = [xdf.DataFrame(index=inp) for inp in inputs]
            concat_df = xdf.concat(empty_dfs, axis=0)
        if getattr(chunk.index_value, "should_be_monotonic", False):
            concat_df.sort_index(inplace=True)
        return concat_df.index

    def _auto_concat_categorical_chunks(_, inputs):
        if len(inputs) == 1:  # pragma: no cover
            return inputs[0]
        else:
            # convert categorical into array
            arrays = [np.asarray(inp) for inp in inputs]
            array = np.concatenate(arrays)
            return pd.Categorical(
                array, categories=inputs[0].categories, ordered=inputs[0].ordered
            )

    chunk = op.outputs[0]
    inputs = [ctx[input.key] for input in op.inputs]

    if isinstance(inputs[0], tuple):
        ctx[chunk.key] = tuple(
            _base_concat(chunk, [input[i] for input in inputs])
            for i in range(len(inputs[0]))
        )
    else:
        ctx[chunk.key] = _base_concat(chunk, inputs)


# Traceback (IndexError)
In [9]: df = md.DataFrame({'a': [1, 2, 3], 'b': [1.1, 2.2, 3.3],
...:                                     'c': [datetime(2020, 1, 1), datetime.now(), datetime(2000, 3, 3, 11, 22, 23)]})
In[10]: df[(df['c'] > md.to_datetime('2020-08-01')) &amp; (df['c'] < md.to_datetime('2020-11-01'))].head().execute()
Traceback (most recent call last):
File "/Users/qinxuye/miniconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-10-d9c9c3710287>", line 1, in <module>
df[(df['c'] > md.to_datetime('2020-08-01')) &amp; (df['c'] < md.to_datetime('2020-11-01'))].head().execute()
File "/Users/qinxuye/Workspace/mars/mars/core.py", line 640, in execute
return run()
File "/Users/qinxuye/Workspace/mars/mars/core.py", line 636, in run
self.data.execute(session, **kw)
File "/Users/qinxuye/Workspace/mars/mars/core.py", line 376, in execute
return run()
File "/Users/qinxuye/Workspace/mars/mars/core.py", line 371, in run
session.run(self, **kw)
File "/Users/qinxuye/Workspace/mars/mars/session.py", line 500, in run
result = self._sess.run(*tileables, **kw)
File "/Users/qinxuye/Workspace/mars/mars/session.py", line 108, in run
res = self._executor.execute_tileables(tileables, **kw)
File "/Users/qinxuye/Workspace/mars/mars/utils.py", line 439, in _inner
return func(*args, **kwargs)
File "/Users/qinxuye/Workspace/mars/mars/executor.py", line 861, in execute_tileables
tileables, tileable_graph=tileable_graph)
File "/Users/qinxuye/Workspace/mars/mars/utils.py", line 439, in _inner
return func(*args, **kwargs)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 348, in build
tileables, tileable_graph=tileable_graph)
File "/Users/qinxuye/Workspace/mars/mars/utils.py", line 439, in _inner
return func(*args, **kwargs)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 262, in build
self._on_tile_failure(tileable_data.op, exc_info)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 301, in inner
raise exc_info[1].with_traceback(exc_info[2]) from None
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 242, in build
tiled = self._tile(tileable_data, tileable_graph)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 337, in _tile
return super()._tile(tileable_data, tileable_graph)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 201, in _tile
tds[0]._inplace_tile()
File "/Users/qinxuye/Workspace/mars/mars/core.py", line 165, in _inplace_tile
return handler.inplace_tile(self)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 136, in inplace_tile
dispatched = self.dispatch(to_tile.op)
File "/Users/qinxuye/Workspace/mars/mars/utils.py", line 439, in _inner
return func(*args, **kwargs)
File "/Users/qinxuye/Workspace/mars/mars/tiles.py", line 119, in dispatch
tiled = op_cls.tile(op)
File "/Users/qinxuye/Workspace/mars/mars/dataframe/arithmetic/core.py", line 261, in tile
return cls._tile_with_tensor(op)
File "/Users/qinxuye/Workspace/mars/mars/dataframe/arithmetic/core.py", line 216, in _tile_with_tensor
rechunk_size = other.nsplits[1] if op.axis == 'columns' or op.axis == 1 else other.nsplits[0]
IndexError: tuple index out of range

# Fixed code
def execute(cls, ctx, op):
    def _base_concat(chunk, inputs):
        # auto generated concat when executing a DataFrame, Series or Index
        if chunk.op.output_types[0] == OutputType.dataframe:
            return _auto_concat_dataframe_chunks(chunk, inputs)
        elif chunk.op.output_types[0] == OutputType.series:
            return _auto_concat_series_chunks(chunk, inputs)
        elif chunk.op.output_types[0] == OutputType.index:
            return _auto_concat_index_chunks(chunk, inputs)
        elif chunk.op.output_types[0] == OutputType.categorical:
            return _auto_concat_categorical_chunks(chunk, inputs)
        else:  # pragma: no cover
            raise TypeError(
                "Only DataFrameChunk, SeriesChunk, IndexChunk, "
                "and CategoricalChunk can be automatically concatenated"
            )

    def _auto_concat_dataframe_chunks(chunk, inputs):
        xdf = (
            pd
            if isinstance(inputs[0], (pd.DataFrame, pd.Series)) or cudf is None
            else cudf
        )

        if chunk.op.axis is not None:
            return xdf.concat(inputs, axis=op.axis)

        # auto generated concat when executing a DataFrame
        if len(inputs) == 1:
            ret = inputs[0]
        else:
            max_rows = max(inp.index[0] for inp in chunk.inputs)
            min_rows = min(inp.index[0] for inp in chunk.inputs)
            n_rows = max_rows - min_rows + 1
            n_cols = int(len(inputs) // n_rows)
            assert n_rows * n_cols == len(inputs)

            concats = []
            for i in range(n_rows):
                if n_cols == 1:
                    concats.append(inputs[i])
                else:
                    concat = xdf.concat(
                        [inputs[i * n_cols + j] for j in range(n_cols)], axis=1
                    )
                    concats.append(concat)

            if xdf is pd:
                # The `sort=False` is to suppress a `FutureWarning` of pandas,
                # when the index or column of chunks to concatenate is not aligned,
                # which may happens for certain ops.
                #
                # See also Note [Columns of Left Join] in test_merge_execution.py.
                ret = xdf.concat(concats, sort=False)
            else:
                ret = xdf.concat(concats)
                # cuDF will lost index name when concat two seriess.
                ret.index.name = concats[0].index.name

        if getattr(chunk.index_value, "should_be_monotonic", False):
            ret.sort_index(inplace=True)
        if getattr(chunk.columns_value, "should_be_monotonic", False):
            ret.sort_index(axis=1, inplace=True)
        return ret

    def _auto_concat_series_chunks(chunk, inputs):
        # auto generated concat when executing a Series
        if all(np.isscalar(inp) for inp in inputs):
            return pd.Series(inputs)
        else:
            if len(inputs) == 1:
                concat = inputs[0]
            else:
                xdf = pd if isinstance(inputs[0], pd.Series) or cudf is None else cudf
                if chunk.op.axis is not None:
                    concat = xdf.concat(inputs, axis=chunk.op.axis)
                else:
                    concat = xdf.concat(inputs)
            if getattr(chunk.index_value, "should_be_monotonic", False):
                concat.sort_index(inplace=True)
            return concat

    def _auto_concat_index_chunks(chunk, inputs):
        if len(inputs) == 1:
            xdf = pd if isinstance(inputs[0], pd.Index) or cudf is None else cudf
            concat_df = xdf.DataFrame(index=inputs[0])
        else:
            xdf = pd if isinstance(inputs[0], pd.Index) or cudf is None else cudf
            empty_dfs = [xdf.DataFrame(index=inp) for inp in inputs]
            concat_df = xdf.concat(empty_dfs, axis=0)
        if getattr(chunk.index_value, "should_be_monotonic", False):
            concat_df.sort_index(inplace=True)
        return concat_df.index

    def _auto_concat_categorical_chunks(_, inputs):
        if len(inputs) == 1:  # pragma: no cover
            return inputs[0]
        else:
            # convert categorical into array
            arrays = [np.asarray(inp) for inp in inputs]
            array = np.concatenate(arrays)
            return pd.Categorical(
                array, categories=inputs[0].categories, ordered=inputs[0].ordered
            )

    chunk = op.outputs[0]
    inputs = [ctx[input.key] for input in op.inputs]

    if isinstance(inputs[0], tuple):
        ctx[chunk.key] = tuple(
            _base_concat(chunk, [input[i] for input in inputs])
            for i in range(len(inputs[0]))
        )
    else:
        ctx[chunk.key] = _base_concat(chunk, inputs)