From be426db01281650f24aaf38131ba970f9e084c74 Mon Sep 17 00:00:00 2001 From: sinhrks Date: Sat, 8 Oct 2016 23:10:01 +0900 Subject: [PATCH] BUG: pivot_table may raise TypeError without values --- doc/source/whatsnew/v0.19.1.txt | 3 +++ pandas/tools/pivot.py | 12 +++++++++--- pandas/tools/tests/test_pivot.py | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 5180b9a092f6c..00171ca9794f0 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -48,3 +48,6 @@ Bug Fixes - Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`) - Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`) - Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`) + +- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` + is not scalar and ``values`` is not specified (:issue:`14380`) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 94b464f6fca6c..9e064a1d1fc99 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -101,10 +101,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', else: values_multi = False values = [values] - else: - values = list(data.columns.drop(keys)) - if values_passed: to_filter = [] for x in keys + values: if isinstance(x, Grouper): @@ -117,6 +114,15 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', if len(to_filter) < len(data.columns): data = data[to_filter] + else: + values = data.columns + for key in keys: + try: + values = values.drop(key) + except (TypeError, ValueError): + pass + values = list(values) + grouped = data.groupby(keys) agged = grouped.agg(aggfunc) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 75c6db23b4bc7..5944fa1b34611 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -131,6 +131,39 @@ def test_pivot_dtypes(self): expected = Series(dict(float64=2)) tm.assert_series_equal(result, expected) + def test_pivot_no_values(self): + # GH 14380 + idx = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-01-02', + '2011-01-01', '2011-01-02']) + df = pd.DataFrame({'A': [1, 2, 3, 4, 5]}, + index=idx) + res = df.pivot_table(index=df.index.month, columns=df.index.day) + + exp_columns = pd.MultiIndex.from_tuples([('A', 1), ('A', 2)]) + exp = pd.DataFrame([[2.5, 4.0], [2.0, np.nan]], + index=[1, 2], columns=exp_columns) + tm.assert_frame_equal(res, exp) + + df = pd.DataFrame({'A': [1, 2, 3, 4, 5], + 'dt': pd.date_range('2011-01-01', freq='D', + periods=5)}, + index=idx) + res = df.pivot_table(index=df.index.month, + columns=pd.Grouper(key='dt', freq='M')) + exp_columns = pd.MultiIndex.from_tuples([('A', + pd.Timestamp('2011-01-31'))]) + exp_columns.names = [None, 'dt'] + exp = pd.DataFrame([3.25, 2.0], + index=[1, 2], columns=exp_columns) + tm.assert_frame_equal(res, exp) + + res = df.pivot_table(index=pd.Grouper(freq='A'), + columns=pd.Grouper(key='dt', freq='M')) + exp = pd.DataFrame([3], + index=pd.DatetimeIndex(['2011-12-31']), + columns=exp_columns) + tm.assert_frame_equal(res, exp) + def test_pivot_multi_values(self): result = pivot_table(self.data, values=['D', 'E'], index='A', columns=['B', 'C'], fill_value=0)