From ef43ad0d892177ed09139276f6819e7a821a203c Mon Sep 17 00:00:00 2001 From: sinhrks Date: Sat, 23 Aug 2014 11:39:50 +0900 Subject: [PATCH] BUG: pivot_table raises KeyError with nameless index and columns --- doc/source/v0.15.0.txt | 2 +- pandas/tools/pivot.py | 2 +- pandas/tools/tests/test_pivot.py | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 4a39dd73da7d0..7f8a8be356e59 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -628,7 +628,7 @@ Bug Fixes - +- Bug in ``pivot_table`` performed with nameless ``index`` and ``columns`` raises ``KeyError`` (:issue:`8103`) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 83df908d8033f..61150f0aeacd0 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -116,7 +116,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', table = agged if table.index.nlevels > 1: - to_unstack = [agged.index.names[i] + to_unstack = [agged.index.names[i] or i for i in range(len(index), len(keys))] table = agged.unstack(to_unstack) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 7e52c8c333dbf..eded5f26f6521 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -527,6 +527,50 @@ def test_pivot_datetime_tz(self): aggfunc=[np.sum, np.mean]) tm.assert_frame_equal(result, expected) + def test_pivot_dtaccessor(self): + # GH 8103 + dates1 = ['2011-07-19 07:00:00', '2011-07-19 08:00:00', '2011-07-19 09:00:00', + '2011-07-19 07:00:00', '2011-07-19 08:00:00', '2011-07-19 09:00:00'] + dates2 = ['2013-01-01 15:00:00', '2013-01-01 15:00:00', '2013-01-01 15:00:00', + '2013-02-01 15:00:00', '2013-02-01 15:00:00', '2013-02-01 15:00:00'] + df = DataFrame({'label': ['a', 'a', 'a', 'b', 'b', 'b'], + 'dt1': dates1, 'dt2': dates2, + 'value1': np.arange(6,dtype='int64'), 'value2': [1, 2] * 3}) + df['dt1'] = df['dt1'].apply(lambda d: pd.Timestamp(d)) + df['dt2'] = df['dt2'].apply(lambda d: pd.Timestamp(d)) + + result = pivot_table(df, index='label', columns=df['dt1'].dt.hour, + values='value1') + + exp_idx = Index(['a', 'b'], name='label') + expected = DataFrame({7: [0, 3], 8: [1, 4], 9:[2, 5]}, + index=exp_idx, columns=[7, 8, 9]) + tm.assert_frame_equal(result, expected) + + result = pivot_table(df, index=df['dt2'].dt.month, columns=df['dt1'].dt.hour, + values='value1') + + expected = DataFrame({7: [0, 3], 8: [1, 4], 9:[2, 5]}, + index=[1, 2], columns=[7, 8, 9]) + tm.assert_frame_equal(result, expected) + + result = pivot_table(df, index=df['dt2'].dt.year, + columns=[df['dt1'].dt.hour, df['dt2'].dt.month], + values='value1') + + exp_col = MultiIndex.from_arrays([[7, 7, 8, 8, 9, 9], [1, 2] * 3]) + expected = DataFrame(np.array([[0, 3, 1, 4, 2, 5]]), + index=[2013], columns=exp_col) + tm.assert_frame_equal(result, expected) + + result = pivot_table(df, index=np.array(['X', 'X', 'X', 'X', 'Y', 'Y']), + columns=[df['dt1'].dt.hour, df['dt2'].dt.month], + values='value1') + expected = DataFrame(np.array([[0, 3, 1, np.nan, 2, np.nan], + [np.nan, np.nan, np.nan, 4, np.nan, 5]]), + index=['X', 'Y'], columns=exp_col) + tm.assert_frame_equal(result, expected) + class TestCrosstab(tm.TestCase):