Skip to content

Commit

Permalink
ERR: raise on missing values in pd.pivot_table #14938 (#14965)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Irv authored and jreback committed Dec 23, 2016
1 parent 45910ae commit 8f7ba1b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,4 @@ Bug Fixes
- Bug in ``pd.unique()`` in which unsigned 64-bit integers were causing overflow (:issue:`14915`)
- 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`)
14 changes: 14 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5746,6 +5746,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])],
Expand Down
5 changes: 5 additions & 0 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 8f7ba1b

Please sign in to comment.