Skip to content

Commit 5dcdfbf

Browse files
Dr-IrvShaharBental
authored andcommitted
ERR: raise on missing values in pd.pivot_table pandas-dev#14938 (pandas-dev#14965)
1 parent 44b7287 commit 5dcdfbf

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,4 @@ Bug Fixes
301301
- Bug in ``pd.unique()`` in which unsigned 64-bit integers were causing overflow (:issue:`14915`)
302302
- Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`)
303303
- Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`)
304+
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)

pandas/tests/groupby/test_groupby.py

+14
Original file line numberDiff line numberDiff line change
@@ -5746,6 +5746,20 @@ def test_group_shift_with_null_key(self):
57465746

57475747
assert_frame_equal(result, expected)
57485748

5749+
def test_pivot_table_values_key_error(self):
5750+
# This test is designed to replicate the error in issue #14938
5751+
df = pd.DataFrame({'eventDate':
5752+
pd.date_range(pd.datetime.today(),
5753+
periods=20, freq='M').tolist(),
5754+
'thename': range(0, 20)})
5755+
5756+
df['year'] = df.set_index('eventDate').index.year
5757+
df['month'] = df.set_index('eventDate').index.month
5758+
5759+
with self.assertRaises(KeyError):
5760+
df.reset_index().pivot_table(index='year', columns='month',
5761+
values='badname', aggfunc='count')
5762+
57495763
def test_agg_over_numpy_arrays(self):
57505764
# GH 3788
57515765
df = pd.DataFrame([[1, np.array([10, 20, 30])],

pandas/tools/pivot.py

+5
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
107107
values_multi = False
108108
values = [values]
109109

110+
# GH14938 Make sure value labels are in data
111+
for i in values:
112+
if i not in data:
113+
raise KeyError(i)
114+
110115
to_filter = []
111116
for x in keys + values:
112117
if isinstance(x, Grouper):

0 commit comments

Comments
 (0)