# Buggy code
def _sparse_reindex(cls, inp, index=None, columns=None):
    if inp.ndim == 2:
        columns = inp.columns if columns is None else columns
        index_shape = len(index) if index is not None else len(inp)
        i_to_columns = dict()

        for i, col in enumerate(columns):
            if col in inp.dtypes:
                if index is None:
                    i_to_columns[i] = inp[col]
                else:
                    indexer = inp.index.reindex(index)[1]
                    cond = indexer >= 0
                    available_indexer = indexer[cond]
                    del indexer
                    data = inp[col].iloc[available_indexer].to_numpy()
                    ind = cond.nonzero()[0]
                    spmatrix = sps.csc_matrix(
                        (data, (ind, np.zeros_like(ind))),
                        shape=(index_shape, 1),
                        dtype=inp[col].dtype,
                    )
                    sparse_array = pd.arrays.SparseArray.from_spmatrix(spmatrix)
                    # convert to SparseDtype(xxx, np.nan)
                    # to ensure 0 in sparse_array not converted to np.nan
                    sparse_array = pd.arrays.SparseArray(
                        sparse_array.sp_values,
                        sparse_index=sparse_array.sp_index,
                        fill_value=np.nan,
                        dtype=pd.SparseDtype(sparse_array.dtype, np.nan),
                    )
                    series = pd.Series(sparse_array, index=index)

                    i_to_columns[i] = series
            else:
                ind = index if index is not None else inp.index
                i_to_columns[i] = pd.DataFrame.sparse.from_spmatrix(
                    sps.coo_matrix((index_shape, 1), dtype=np.float64), index=ind
                ).iloc[:, 0]

        df = pd.DataFrame(i_to_columns)
        df.columns = columns
        return df
    else:
        indexer = inp.index.reindex(index)[1]
        spmatrix = sps.lil_matrix((len(index), 1), dtype=inp.dtype)
        cond = indexer >= 0
        available_indexer = indexer[cond]
        del indexer
        spmatrix[cond, 0] = inp.iloc[available_indexer].to_numpy()
        series = pd.DataFrame.sparse.from_spmatrix(spmatrix, index=index).iloc[:, 0]
        series.name = inp.name
        return series


# Traceback (NotImplementedError)
In [52]: df = md.DataFrame(index=[1, 2, 3])

In [53]: df['a'] = md.Series(['a', 'b', 'c'], index=[2, 3, 4])
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-53-f550a59ef82c> in <module>
----> 1 df['a'] = md.Series(['a', 'b', 'c'], index=[2, 3, 4])

~/Workspace/mars/mars/dataframe/indexing/setitem.py in dataframe_setitem(df, col, value)
182 def dataframe_setitem(df, col, value):
183     op = DataFrameSetitem(target=df, indexes=col, value=value)
--> 184     return op(df, value)

~/Workspace/mars/mars/utils.py in _inner(*args, **kwargs)
457         @functools.wraps(func)
458         def _inner(*args, **kwargs):
--> 459             with self:
460                 return func(*args, **kwargs)
461

~/Workspace/mars/mars/dataframe/indexing/setitem.py in __call__(self, target, value)
83
84             if value.index_value.key != target.index_value.key:  # pragma: no cover
---> 85                 raise NotImplementedError('Does not support setting value '
86                                           'with different index for now')
87

NotImplementedError: Does not support setting value with different index for now

# Fixed code
def _sparse_reindex(cls, inp, index=None, columns=None):
    if inp.ndim == 2:
        columns = inp.columns if columns is None else columns
        index_shape = len(index) if index is not None else len(inp)
        i_to_columns = dict()

        for i, col in enumerate(columns):
            if col in inp.dtypes:
                if index is None:
                    i_to_columns[i] = inp[col]
                else:
                    indexer = inp.index.reindex(index)[1]
                    cond = indexer >= 0
                    available_indexer = indexer[cond]
                    del indexer
                    data = inp[col].iloc[available_indexer].to_numpy()
                    ind = cond.nonzero()[0]
                    spmatrix = sps.csc_matrix(
                        (data, (ind, np.zeros_like(ind))),
                        shape=(index_shape, 1),
                        dtype=inp[col].dtype,
                    )
                    sparse_array = pd.arrays.SparseArray.from_spmatrix(spmatrix)
                    # convert to SparseDtype(xxx, np.nan)
                    # to ensure 0 in sparse_array not converted to np.nan
                    sparse_array = pd.arrays.SparseArray(
                        sparse_array.sp_values,
                        sparse_index=sparse_array.sp_index,
                        fill_value=np.nan,
                        dtype=pd.SparseDtype(sparse_array.dtype, np.nan),
                    )
                    series = pd.Series(sparse_array, index=index)

                    i_to_columns[i] = series
            else:
                ind = index if index is not None else inp.index
                i_to_columns[i] = pd.DataFrame.sparse.from_spmatrix(
                    sps.coo_matrix((index_shape, 1), dtype=np.float64), index=ind
                ).iloc[:, 0]

        df = pd.DataFrame(i_to_columns)
        df.columns = columns
        return df
    else:
        indexer = inp.index.reindex(index)[1]
        cond = indexer >= 0
        available_indexer = indexer[cond]
        del indexer
        data = inp.iloc[available_indexer].to_numpy()
        ind = cond.nonzero()[0]
        spmatrix = sps.csc_matrix(
            (data, (ind, np.zeros_like(ind))), shape=(len(index), 1), dtype=inp.dtype
        )
        sparse_array = pd.arrays.SparseArray.from_spmatrix(spmatrix)
        # convert to SparseDtype(xxx, np.nan)
        # to ensure 0 in sparse_array not converted to np.nan
        sparse_array = pd.arrays.SparseArray(
            sparse_array.sp_values,
            sparse_index=sparse_array.sp_index,
            fill_value=np.nan,
            dtype=pd.SparseDtype(sparse_array.dtype, np.nan),
        )
        series = pd.Series(sparse_array, index=index, name=inp.name)
        return series