Skip to content

Commit 1aec805

Browse files
committed
fixed bug in df.aggregate passing non-existent columns
1 parent 93c86aa commit 1aec805

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ Groupby/Resample/Rolling
646646
- Bug in :func:`DataFrame.groupby` where tuples were interpreted as lists of keys rather than as keys (:issue:`17979`, :issue:`18249`)
647647
- Bug in :func:`DataFrame.transform` where particular aggregation functions were being incorrectly cast to match the dtype(s) of the grouped data (:issue:`19200`)
648648
- Bug in :func:`DataFrame.groupby` passing the `on=` kwarg, and subsequently using ``.apply()`` (:issue:`17813`)
649+
- Bug in :func:`DataFrame.aggregate` on non-existant columns (:issue:`16766`)
649650

650651
Sparse
651652
^^^^^^

pandas/core/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ def nested_renaming_depr(level=4):
371371
else:
372372
new_arg[k] = v
373373

374+
if isinstance(obj, ABCDataFrame) and k not in obj.columns:
375+
raise ValueError(
376+
"Column '{col}' does not exist!".format(col=k))
377+
374378
# the keys must be in the columns
375379
# for ndim=2, or renamers for ndim=1
376380

pandas/tests/test_resample.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def f():
614614
t[['A']].agg({'A': ['sum', 'std'],
615615
'B': ['mean', 'std']})
616616

617-
pytest.raises(SpecificationError, f)
617+
pytest.raises(ValueError, f)
618618

619619
def test_agg_nested_dicts(self):
620620

@@ -659,6 +659,21 @@ def f():
659659
'B': {'rb': ['mean', 'std']}})
660660
assert_frame_equal(result, expected, check_like=True)
661661

662+
def test_try_aggregate_non_existing_column(self):
663+
# GH 16766
664+
data = [
665+
{'dt': datetime(2017, 6, 1, 0), 'x': 1.0, 'y': 2.0},
666+
{'dt': datetime(2017, 6, 1, 1), 'x': 2.0, 'y': 2.0},
667+
{'dt': datetime(2017, 6, 1, 2), 'x': 3.0, 'y': 1.5}
668+
]
669+
df = DataFrame(data).set_index('dt')
670+
671+
# Error as we don't have 'z' column
672+
with pytest.raises(ValueError):
673+
df.resample('30T').agg({'x': ['mean'],
674+
'y': ['median'],
675+
'z': ['sum']})
676+
662677
def test_selection_api_validation(self):
663678
# GH 13500
664679
index = date_range(datetime(2005, 1, 1),

0 commit comments

Comments
 (0)