diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 0223a11d8a011..548f23df532e2 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -548,8 +548,7 @@ Bug Fixes - Bug in ``read_html`` where ``bytes`` objects were not tested for in ``_read`` (:issue:`7927`). - - +- Bug in ``DataFrame.stack()`` when one of the column levels was a datelike (:issue: `8039`) diff --git a/pandas/core/reshape.py b/pandas/core/reshape.py index b014ede6e65a8..f2817e04819bb 100644 --- a/pandas/core/reshape.py +++ b/pandas/core/reshape.py @@ -601,7 +601,7 @@ def _stack_multi_columns(frame, level=-1, dropna=True): # tuple list excluding level for grouping columns if len(frame.columns.levels) > 2: tuples = list(zip(*[ - lev.values.take(lab) for lev, lab in + lev.take(lab) for lev, lab in zip(this.columns.levels[:-1], this.columns.labels[:-1]) ])) unique_groups = [key for key, _ in itertools.groupby(tuples)] diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index cf845a18092af..d1e6a2bf59303 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -11857,6 +11857,17 @@ def test_unstack_non_unique_index_names(self): with tm.assertRaises(ValueError): df.T.stack('c1') + def test_stack_datetime_column_multiIndex(self): + # GH 8039 + t = datetime(2014, 1, 1) + df = DataFrame([1, 2, 3, 4], columns=MultiIndex.from_tuples([(t, 'A', 'B')])) + result = df.stack() + + eidx = MultiIndex.from_product([(0, 1, 2, 3), ('B',)]) + ecols = MultiIndex.from_tuples([(t, 'A')]) + expected = DataFrame([1, 2, 3, 4], index=eidx, columns=ecols) + assert_frame_equal(result, expected) + def test_repr_with_mi_nat(self): df = DataFrame({'X': [1, 2]}, index=[[pd.NaT, pd.Timestamp('20130101')], ['a', 'b']])