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 1 commit
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 @@ -84,7 +84,7 @@ Other API changes
- Added :meth:`DataFrame.value_counts` (:issue:`5377`)
- :meth:`Groupby.groups` now returns an abbreviated representation when called on large dataframes (:issue:`1135`)
- ``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`)
-
- :meth:`DataFrame.agg` now provides more descriptive SpecificationError message when attempting to aggregating non-existant column (:issue:`32755`)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this to reshaping


Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ 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

if len(cols) > 1:
raise SpecificationError(f"Columns {cols} do not exist")
else:
raise SpecificationError(f"Column {cols} does 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"Columns \['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 \['ma'\] does 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"Columns \['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"Columns \['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