Skip to content

Commit 5adfc89

Browse files
JDTruj2018SeeminSyed
authored andcommitted
ERR: Better error message for missing columns in aggregate (pandas-dev#32836)
1 parent a88d24c commit 5adfc89

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ Reshaping
385385
- :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`)
386386
- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`)
387387
- Bug in :meth:`DataFrame.pivot_table` losing timezone information when creating a :class:`MultiIndex` level from a column with timezone-aware dtype (:issue:`32558`)
388-
388+
- :meth:`DataFrame.agg` now provides more descriptive ``SpecificationError`` message when attempting to aggregating non-existant column (:issue:`32755`)
389389

390390
Sparse
391391
^^^^^^

pandas/core/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ def _aggregate(self, arg, *args, **kwargs):
356356
if isinstance(obj, ABCDataFrame) and len(
357357
obj.columns.intersection(keys)
358358
) != len(keys):
359-
raise SpecificationError("nested renamer is not supported")
359+
cols = sorted(set(keys) - set(obj.columns.intersection(keys)))
360+
raise SpecificationError(f"Column(s) {cols} do not exist")
360361

361362
from pandas.core.reshape.concat import concat
362363

pandas/tests/groupby/aggregate/test_other.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def test_aggregate_api_consistency():
209209
expected = pd.concat([c_mean, c_sum, d_mean, d_sum], axis=1)
210210
expected.columns = MultiIndex.from_product([["C", "D"], ["mean", "sum"]])
211211

212-
msg = r"nested renamer is not supported"
212+
msg = r"Column\(s\) \['r', 'r2'\] do not exist"
213213
with pytest.raises(SpecificationError, match=msg):
214214
grouped[["D", "C"]].agg({"r": np.sum, "r2": np.mean})
215215

@@ -224,9 +224,11 @@ def test_agg_dict_renaming_deprecation():
224224
{"B": {"foo": ["sum", "max"]}, "C": {"bar": ["count", "min"]}}
225225
)
226226

227+
msg = r"Column\(s\) \['ma'\] do not exist"
227228
with pytest.raises(SpecificationError, match=msg):
228229
df.groupby("A")[["B", "C"]].agg({"ma": "max"})
229230

231+
msg = r"nested renamer is not supported"
230232
with pytest.raises(SpecificationError, match=msg):
231233
df.groupby("A").B.agg({"foo": "count"})
232234

pandas/tests/resample/test_resample_api.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def test_agg_consistency():
287287

288288
r = df.resample("3T")
289289

290-
msg = "nested renamer is not supported"
290+
msg = r"Column\(s\) \['r1', 'r2'\] do not exist"
291291
with pytest.raises(pd.core.base.SpecificationError, match=msg):
292292
r.agg({"r1": "mean", "r2": "sum"})
293293

@@ -419,7 +419,7 @@ def test_agg_misc():
419419
[("result1", "A"), ("result1", "B"), ("result2", "A"), ("result2", "B")]
420420
)
421421

422-
msg = "nested renamer is not supported"
422+
msg = r"Column\(s\) \['result1', 'result2'\] do not exist"
423423
for t in cases:
424424
with pytest.raises(pd.core.base.SpecificationError, match=msg):
425425
t[["A", "B"]].agg(OrderedDict([("result1", np.sum), ("result2", np.mean)]))
@@ -440,6 +440,8 @@ def test_agg_misc():
440440
result = t[["A", "B"]].agg({"A": ["sum", "std"], "B": ["mean", "std"]})
441441
tm.assert_frame_equal(result, expected, check_like=True)
442442

443+
msg = "nested renamer is not supported"
444+
443445
# series like aggs
444446
for t in cases:
445447
with pytest.raises(pd.core.base.SpecificationError, match=msg):

0 commit comments

Comments
 (0)