Skip to content

ERR: Better error message for missing columns in aggregate #32836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ Reshaping
- :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`)
- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`)
- Bug in :meth:`DataFrame.pivot_table` losing timezone information when creating a :class:`MultiIndex` level from a column with timezone-aware dtype (:issue:`32558`)

- :meth:`DataFrame.agg` now provides more descriptive ``SpecificationError`` message when attempting to aggregating non-existant column (:issue:`32755`)

Sparse
^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ def _aggregate(self, arg, *args, **kwargs):
if isinstance(obj, ABCDataFrame) and len(
obj.columns.intersection(keys)
) != len(keys):
raise SpecificationError("nested renamer is not supported")
cols = sorted(set(keys) - set(obj.columns.intersection(keys)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not worth it dispatching on both, just make. single message

raise SpecificationError(f"Column(s) {cols} do not exist")

from pandas.core.reshape.concat import concat

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/groupby/aggregate/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_aggregate_api_consistency():
expected = pd.concat([c_mean, c_sum, d_mean, d_sum], axis=1)
expected.columns = MultiIndex.from_product([["C", "D"], ["mean", "sum"]])

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

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

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

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

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def test_agg_consistency():

r = df.resample("3T")

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

Expand Down Expand Up @@ -419,7 +419,7 @@ def test_agg_misc():
[("result1", "A"), ("result1", "B"), ("result2", "A"), ("result2", "B")]
)

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

msg = "nested renamer is not supported"

# series like aggs
for t in cases:
with pytest.raises(pd.core.base.SpecificationError, match=msg):
Expand Down