Skip to content

Commit 5f0ca8a

Browse files
committed
Issue 32755: More descriptive SpecificationError message that reports to user non-existing columns causing error
Line too long - corrected What's New Updated Tests Fixed testing errors
1 parent bfa29f1 commit 5f0ca8a

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Other API changes
8484
- Added :meth:`DataFrame.value_counts` (:issue:`5377`)
8585
- :meth:`Groupby.groups` now returns an abbreviated representation when called on large dataframes (:issue:`1135`)
8686
- ``loc`` lookups with an object-dtype :class:`Index` and an integer key will now raise ``KeyError`` instead of ``TypeError`` when key is missing (:issue:`31905`)
87-
-
87+
- :meth:`DataFrame.agg` now provides more descriptive SpecificationError message when attempting to aggregating non-existant column (:issue:`32755`)
8888

8989
Backwards incompatible API changes
9090
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pandas/core/base.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,11 @@ 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+
if len(cols) > 1:
361+
raise SpecificationError(f"Columns {cols} do not exist")
362+
else:
363+
raise SpecificationError(f"Column {cols} does not exist")
360364

361365
from pandas.core.reshape.concat import concat
362366

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"Columns \['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 \['ma'\] does 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"Columns \['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"Columns \['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)