BUG: Unstacking MultiIndex with datetime tz-aware data raises ValueError: cannot create a DatetimeTZBlock without a tz · Issue #18338 · pandas-dev/pandas (original) (raw)

Exception thrown. This used to work pre 0.21 and works with non Timestamp columns

ValueError                                Traceback (most recent call last)
<ipython-input-13-469280bd9dc1> in <module>()
      5     'c': ['c'],
      6 })
----> 7 df.set_index(['a', 'b']).unstack()

/usr/local/lib/python3.5/dist-packages/pandas/core/frame.py in unstack(self, level, fill_value)
   4544         """
   4545         from pandas.core.reshape.reshape import unstack
-> 4546         return unstack(self, level, fill_value)
   4547 
   4548     _shared_docs['melt'] = ("""

/usr/local/lib/python3.5/dist-packages/pandas/core/reshape/reshape.py in unstack(obj, level, fill_value)
    467     if isinstance(obj, DataFrame):
    468         if isinstance(obj.index, MultiIndex):
--> 469             return _unstack_frame(obj, level, fill_value=fill_value)
    470         else:
    471             return obj.T.stack(dropna=False)

/usr/local/lib/python3.5/dist-packages/pandas/core/reshape/reshape.py in _unstack_frame(obj, level, fill_value)
    480         unstacker = partial(_Unstacker, index=obj.index,
    481                             level=level, fill_value=fill_value)
--> 482         blocks = obj._data.unstack(unstacker)
    483         klass = type(obj)
    484         return klass(blocks)

/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py in unstack(self, unstacker_func)
   4337                 partial(unstacker_func,
   4338                         value_columns=self.items[blk.mgr_locs.indexer]),
-> 4339                 new_columns)
   4340 
   4341             new_blocks.extend(blocks)

/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py in _unstack(self, unstacker_func, new_columns)
   1808 
   1809         blocks = [self.make_block_same_class(vals, [place])
-> 1810                   for vals, place in zip(new_values, new_placement)]
   1811         return blocks, mask
   1812 

/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py in <listcomp>(.0)
   1808 
   1809         blocks = [self.make_block_same_class(vals, [place])
-> 1810                   for vals, place in zip(new_values, new_placement)]
   1811         return blocks, mask
   1812 

/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py in make_block_same_class(self, values, placement, fastpath, **kwargs)
    222             placement = self.mgr_locs
    223         return make_block(values, placement=placement, klass=self.__class__,
--> 224                           fastpath=fastpath, **kwargs)
    225 
    226     @mgr_locs.setter

/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py in make_block(values, placement, klass, ndim, dtype, fastpath)
   2948     elif klass is DatetimeTZBlock and not is_datetimetz(values):
   2949         return klass(values, ndim=ndim, fastpath=fastpath,
-> 2950                      placement=placement, dtype=dtype)
   2951 
   2952     return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)

/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py in __init__(self, values, placement, ndim, **kwargs)
   2610 
   2611         if values.tz is None:
-> 2612             raise ValueError("cannot create a DatetimeTZBlock without a tz")
   2613 
   2614         super(DatetimeTZBlock, self).__init__(values, placement=placement,

ValueError: cannot create a DatetimeTZBlock without a tz
   c timestamp
b  b         b
a             
a  c       2017-08-27 01:00:00.709949+00:00