diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index e7b2fc5a6505d..0b13d78f0377e 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -300,3 +300,4 @@ Bug Fixes - Bug in ``Series.unique()`` in which unsigned 64-bit integers were causing overflow (:issue:`14721`) - Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`) - Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`) +- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index f8d9d73590a60..f609de23d9189 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -5725,6 +5725,20 @@ def test_group_shift_with_null_key(self): assert_frame_equal(result, expected) + def test_pivot_table_values_key_error(self): + # This test is designed to replicate the error in issue #14938 + df = pd.DataFrame({'eventDate': + pd.date_range(pd.datetime.today(), + periods=20, freq='M').tolist(), + 'thename': range(0, 20)}) + + df['year'] = df.set_index('eventDate').index.year + df['month'] = df.set_index('eventDate').index.month + + with self.assertRaises(KeyError): + df.reset_index().pivot_table(index='year', columns='month', + values='badname', aggfunc='count') + def test_agg_over_numpy_arrays(self): # GH 3788 df = pd.DataFrame([[1, np.array([10, 20, 30])], diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 820a545363ee3..0f56b0b076897 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -107,6 +107,11 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', values_multi = False values = [values] + # GH14938 Make sure value labels are in data + for i in values: + if i not in data: + raise KeyError(i) + to_filter = [] for x in keys + values: if isinstance(x, Grouper):