# Buggy code
def get_result(self):
    # TODO: find a better way than this masking business

    values, value_mask = self.get_new_values()
    columns = self.get_new_columns()
    index = self.get_new_index()

    # filter out missing levels
    if values.shape[1] > 0:
        col_inds, obs_ids = compress_group_index(self.sorted_labels[-1])
        # rare case, level values not observed
        if len(obs_ids) < self.full_shape[1]:
            inds = (value_mask.sum(0) > 0).nonzero()[0]
            values = algos.take_nd(values, inds, axis=1)
            columns = columns[inds]

    # may need to coerce categoricals here
    if self.is_categorical is not None:
        categories = self.is_categorical.categories
        ordered = self.is_categorical.ordered
        values = [
            Categorical(values[:, i], categories=categories, ordered=ordered)
            for i in range(values.shape[-1])
        ]

    return self.constructor(values, index=index, columns=columns)


# Traceback (AssertionError)
In [2]: df = pd.DataFrame([[2009, 1, 15, 'City1', 'Housing'],
...:                    [2010, 2, np.nan, 'City2', 'Housing']],
...:                   columns=['year', 'month', 'day', 'city', 'sector'])
...:

In [3]: df['day'] = 15
...: df['date'] = pd.to_datetime(df[['year', 'month', 'day']])
...:

In [4]: ind = df.set_index(['date', 'sector', 'city'], drop=False).sort_index()

In [5]:

In [5]: cols = ['month', 'year', 'sector']
...: per_city = ind.loc[(slice(None), slice(None), 'City1'), cols].dropna(how='any')
...:

In [6]: per_city.unstack()
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-97028d1092eb> in <module>()
----> 1 per_city.unstack()

/home/nobackup/repo/pandas/pandas/core/frame.py in unstack(self, level, fill_value)
4575         """
4576         from pandas.core.reshape.reshape import unstack
-> 4577         return unstack(self, level, fill_value)
4578
4579     _shared_docs['melt'] = ("""

/home/nobackup/repo/pandas/pandas/core/reshape/reshape.py in unstack(obj, level, fill_value)
463     if isinstance(obj, DataFrame):
464         if isinstance(obj.index, MultiIndex):
--> 465             return _unstack_frame(obj, level, fill_value=fill_value)
466         else:
467             return obj.T.stack(dropna=False)

/home/nobackup/repo/pandas/pandas/core/reshape/reshape.py in _unstack_frame(obj, level, fill_value)
476         unstacker = partial(_Unstacker, index=obj.index,
477                             level=level, fill_value=fill_value)
--> 478         blocks = obj._data.unstack(unstacker)
479         klass = type(obj)
480         return klass(blocks)

/home/nobackup/repo/pandas/pandas/core/internals.py in unstack(self, unstacker_func)
4374         new_columns = new_columns[columns_mask]
4375
-> 4376         bm = BlockManager(new_blocks, [new_columns, new_index])
4377         return bm
4378

/home/nobackup/repo/pandas/pandas/core/internals.py in __init__(self, blocks, axes, do_integrity_check, fastpath)
3042         self._consolidate_check()
3043
-> 3044         self._rebuild_blknos_and_blklocs()
3045
3046     def make_empty(self, axes=None):

/home/nobackup/repo/pandas/pandas/core/internals.py in _rebuild_blknos_and_blklocs(self)
3134
3135         if (new_blknos == -1).any():
-> 3136             raise AssertionError("Gaps in blk ref_locs")
3137
3138         self._blknos = new_blknos

AssertionError: Gaps in blk ref_locs

# Fixed code
def get_result(self):
    values, _ = self.get_new_values()
    columns = self.get_new_columns()
    index = self.get_new_index()

    # may need to coerce categoricals here
    if self.is_categorical is not None:
        categories = self.is_categorical.categories
        ordered = self.is_categorical.ordered
        values = [
            Categorical(values[:, i], categories=categories, ordered=ordered)
            for i in range(values.shape[-1])
        ]

    return self.constructor(values, index=index, columns=columns)