Repository: pandas-dev/pandas

Issue:
BUG: multi-index with one level with a tz buggy
related: #7791, #7792 (and I think this causes both)
this is a dupe of #7746 

```
In [8]: df = DataFrame(np.arange(6).reshape(6,1),index=MultiIndex.from_product([['a','b'],date_range('20130101',periods=3,tz='Europe/Berlin')]))

In [9]: df
Out[9]: 
                             0
a 2013-01-01 00:00:00+01:00  0
  2013-01-02 00:00:00+01:00  1
  2013-01-03 00:00:00+01:00  2
b 2013-01-01 00:00:00+01:00  3
  2013-01-02 00:00:00+01:00  4
  2013-01-03 00:00:00+01:00  5

In [10]: df.reset_index()

ValueError: Length of values does not match length of index
```



Discussion:
xref #7792 

cc @sinhrks


Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -2452,13 +2452,13 @@ def _maybe_casted_values(index, labels=None):
                 if values.dtype == np.object_:
                     values = lib.maybe_convert_objects(values)
 
-                # if we have the labels, extract the values with a mask
-                if labels is not None:
-                    mask = labels == -1
-                    values = values.take(labels)
-                    if mask.any():
-                        values, changed = com._maybe_upcast_putmask(values,
-                                                                    mask, np.nan)
+            # if we have the labels, extract the values with a mask
+            if labels is not None:
+                mask = labels == -1
+                values = values.take(labels)
+                if mask.any():
+                    values, changed = com._maybe_upcast_putmask(values,
+                                                                mask, np.nan)
             return values
 
         new_index = np.arange(len(new_obj))


Test patch:
diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py
--- a/pandas/tests/test_multilevel.py
+++ b/pandas/tests/test_multilevel.py
@@ -2118,6 +2118,33 @@ def test_reset_index_datetime(self):
             expected['idx3'] = expected['idx3'].apply(lambda d: pd.Timestamp(d, tz='Europe/Paris'))
             assert_frame_equal(df.reset_index(), expected)
 
+            # GH 7793
+            idx = pd.MultiIndex.from_product([['a','b'], pd.date_range('20130101', periods=3, tz=tz)])
+            df = pd.DataFrame(np.arange(6).reshape(6,1), columns=['a'], index=idx)
+
+            expected = pd.DataFrame({'level_0': 'a a a b b b'.split(),
+                                     'level_1': [datetime.datetime(2013, 1, 1),
+                                                 datetime.datetime(2013, 1, 2),
+                                                 datetime.datetime(2013, 1, 3)] * 2, 
+                                     'a': np.arange(6, dtype='int64')},
+                                     columns=['level_0', 'level_1', 'a'])
+            expected['level_1'] = expected['level_1'].apply(lambda d: pd.Timestamp(d, offset='D', tz=tz))
+            assert_frame_equal(df.reset_index(), expected)
+
+    def test_reset_index_period(self):
+        # GH 7746
+        idx = pd.MultiIndex.from_product([pd.period_range('20130101', periods=3, freq='M'),
+                                         ['a','b','c']], names=['month', 'feature'])
+
+        df = pd.DataFrame(np.arange(9).reshape(-1,1), index=idx, columns=['a'])
+        expected = pd.DataFrame({'month': [pd.Period('2013-01', freq='M')] * 3 +
+                                          [pd.Period('2013-02', freq='M')] * 3 +
+                                          [pd.Period('2013-03', freq='M')] * 3,
+                                 'feature': ['a', 'b', 'c'] * 3,
+                                 'a': np.arange(9, dtype='int64')},
+                                columns=['month', 'feature', 'a'])
+        assert_frame_equal(df.reset_index(), expected)
+
     def test_set_index_period(self):
         # GH 6631
         df = DataFrame(np.random.random(6))