# Buggy code
def _execute_dataframe_map(cls, ctx, op):
    a, pivots = [ctx[c.key] for c in op.inputs]
    out = op.outputs[0]

    # use numpy.searchsorted to find split positions.
    by = op.by

    distinct_col = (
        _PSRS_DISTINCT_COL
        if a.columns.nlevels == 1
        else (_PSRS_DISTINCT_COL,) + ("",) * (a.columns.nlevels - 1)
    )
    if distinct_col in a.columns:
        by = list(by) + [distinct_col]

    records = a[by].to_records(index=False)
    p_records = pivots.to_records(index=False)
    if op.ascending:
        poses = records.searchsorted(p_records, side="right")
    else:
        poses = len(records) - records[::-1].searchsorted(p_records, side="right")
    del records, p_records

    poses = (None,) + tuple(poses) + (None,)
    for i in range(op.n_partition):
        values = a.iloc[poses[i] : poses[i + 1]]
        ctx[(out.key, str(i))] = values


# Traceback (ZeroDivisionError)
Traceback (most recent call last):
File "/Users/wenjun.swj/miniconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-6-f86c22f8381d>", line 1, in <module>
mdf[mdf[0] != 0].sort_values(0).execute()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 646, in execute
return run()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 642, in run
self.data.execute(session, **kw)
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 379, in execute
return run()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 374, in run
session.run(self, **kw)
File "/Users/wenjun.swj/Code/mars/mars/session.py", line 505, in run
result = self._sess.run(*tileables, **kw)
File "/Users/wenjun.swj/Code/mars/mars/session.py", line 111, in run
res = self._executor.execute_tileables(tileables, **kw)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 457, in _inner
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 883, in execute_tileables
self.execute_graph(chunk_graph, list(temp_result_keys),
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 698, in execute_graph
res = graph_execution.execute(retval)
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 579, in execute
future.result()
File "/Users/wenjun.swj/miniconda3/lib/python3.8/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/Users/wenjun.swj/miniconda3/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
raise self._exception
File "/Users/wenjun.swj/miniconda3/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 457, in _inner
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 446, in _execute_operand
self.handle_op(first_op, results, self._mock)
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 378, in handle_op
return Executor.handle(*args, **kw)
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 649, in handle
return runner(results, op)
File "/Users/wenjun.swj/Code/mars/mars/dataframe/sort/psrs.py", line 355, in execute
num = n // a.shape[op.axis] + 1
ZeroDivisionError: integer division or modulo by zero

# Fixed code
def _execute_dataframe_map(cls, ctx, op):
    a, pivots = [ctx[c.key] for c in op.inputs]
    out = op.outputs[0]

    if len(a) == 0:
        # when the chunk is empty, no slices can be produced
        for i in range(op.n_partition):
            ctx[(out.key, str(i))] = a
        return

    # use numpy.searchsorted to find split positions.
    by = op.by

    distinct_col = (
        _PSRS_DISTINCT_COL
        if a.columns.nlevels == 1
        else (_PSRS_DISTINCT_COL,) + ("",) * (a.columns.nlevels - 1)
    )
    if distinct_col in a.columns:
        by = list(by) + [distinct_col]

    try:
        poses = cls._calc_poses(a[by], pivots, op.ascending)
    except TypeError:
        poses = cls._calc_poses(
            a[by].fillna(_largest), pivots.fillna(_largest), op.ascending
        )

    poses = (None,) + tuple(poses) + (None,)
    for i in range(op.n_partition):
        values = a.iloc[poses[i] : poses[i + 1]]
        ctx[(out.key, str(i))] = values