Repository: pandas-dev/pandas

Issue:
ERR: No Error when values argument in pivot_table is not in df.columns
#### Code Sample

```python
import pandas as pd

test = pd.DataFrame({'Date':pd.date_range(pd.datetime.today(), periods=20,freq='M').tolist(),
'occurrenceID':range(0,20)})

test['year'] = test.set_index('eventDate').index.year
test['month'] = test.set_index('eventDate').index.month

heatmap_prep = test.reset_index().pivot_table(index='year',columns='month',values='occurenceID',aggfunc='count')
```
#### Problem description

The above code gives no error, while 'occurenceID' is not a dataframe column (as there is a typo, needs to be 'occurrenceID', with two r's). It just produces a dataframe with only an index. Maybe a KeyError should be thrown?

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS  
------------------  
commit: None  
python: 3.5.2.final.0  
python-bits: 64  
OS: Darwin  
OS-release: 14.5.0  
machine: x86_64  
processor: i386  
byteorder: little  
LC_ALL: en_US.UTF-8  
LANG: None  
LOCALE: en_US.UTF-8  

pandas: 0.19.1
nose: None
pip: 9.0.1
setuptools: 32.1.0.post20161217
Cython: None
numpy: 1.11.2
scipy: 0.18.1
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.3
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

</details>



Discussion:
yep, looks like a bug. These should be validated and a ``KeyError`` if not found (with a helpful message, iow which arg its coming from).
Looks like the `KeyError` is generated for when the `index` and `columns` arguments have a bad name. So it is only for `values` that there is an issue.

Resolving patch:
diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py
--- 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):


Test patch:
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
--- 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])],