Skip to content

Commit d142bc1

Browse files
committed
fixed bug in df.aggregate passing non-existent columns
1 parent 983d71f commit d142bc1

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ Groupby/Resample/Rolling
647647
- Bug in :func:`DataFrame.groupby` where aggregation by ``first``/``last``/``min``/``max`` was causing timestamps to lose precision (:issue:`19526`)
648648
- Bug in :func:`DataFrame.transform` where particular aggregation functions were being incorrectly cast to match the dtype(s) of the grouped data (:issue:`19200`)
649649
- Bug in :func:`DataFrame.groupby` passing the `on=` kwarg, and subsequently using ``.apply()`` (:issue:`17813`)
650+
- Bug in :func:`DataFrame.resample().aggregate` not raising a `ValueError` when aggregating a non-existent column (:issue:`16766`)
650651

651652
Sparse
652653
^^^^^^

pandas/core/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ def nested_renaming_depr(level=4):
392392

393393
elif isinstance(obj, ABCSeries):
394394
nested_renaming_depr()
395+
elif isinstance(obj, ABCDataFrame) and \
396+
k not in obj.columns:
397+
raise ValueError(
398+
"Column '{col}' does not exist!".format(col=k))
395399

396400
arg = new_arg
397401

pandas/tests/test_resample.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from pandas.core.dtypes.generic import ABCSeries, ABCDataFrame
2222
from pandas.compat import range, lrange, zip, product, OrderedDict
23-
from pandas.core.base import SpecificationError
2423
from pandas.errors import UnsupportedFunctionCall
2524
from pandas.core.groupby import DataError
2625
import pandas.core.common as com
@@ -614,7 +613,7 @@ def f():
614613
t[['A']].agg({'A': ['sum', 'std'],
615614
'B': ['mean', 'std']})
616615

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

619618
def test_agg_nested_dicts(self):
620619

@@ -659,6 +658,21 @@ def f():
659658
'B': {'rb': ['mean', 'std']}})
660659
assert_frame_equal(result, expected, check_like=True)
661660

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

0 commit comments

Comments
 (0)