From 13cc277c3f10fd30e6b8f648ef46fda08489c727 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 30 Apr 2024 21:30:42 +0800 Subject: [PATCH 1/6] DOC: Improve error msg for --- pandas/core/apply.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 832beeddcef3c..c6e7e7ade674c 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -628,7 +628,11 @@ def normalize_dictlike_arg( cols = Index(list(func.keys())).difference(obj.columns, sort=True) if len(cols) > 0: - raise KeyError(f"Column(s) {list(cols)} do not exist") + raise KeyError( + f"Column(s) {list(cols)} do not exist, " + "if you don't mean to apply to each rows, " + "consider using `axis=0` instead." + ) aggregator_types = (list, tuple, dict) From 3c08734903f7b3d3e3acef2dbdfb5804b55c7c0b Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Sat, 4 May 2024 11:35:23 +0800 Subject: [PATCH 2/6] Improve error msg --- pandas/core/apply.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index c6e7e7ade674c..a82aef1ab111e 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -628,11 +628,7 @@ def normalize_dictlike_arg( cols = Index(list(func.keys())).difference(obj.columns, sort=True) if len(cols) > 0: - raise KeyError( - f"Column(s) {list(cols)} do not exist, " - "if you don't mean to apply to each rows, " - "consider using `axis=0` instead." - ) + raise KeyError(f"Row(s) or Column(s) {list(cols)} do not exist") aggregator_types = (list, tuple, dict) From 686ac922e080bccb86b4509ff221993e790d1704 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Sat, 4 May 2024 14:11:46 +0800 Subject: [PATCH 3/6] Add test --- pandas/tests/apply/test_invalid_arg.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index b5ad1094f5bf5..8f9c3bb1336b2 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -359,3 +359,15 @@ def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper): msg = "Function did not transform" with pytest.raises(ValueError, match=msg): obj.transform(op) + + +def test_transform_axis_raises(): + # GH 58474 + df = DataFrame({"foo": [2, 4, 6], "bar": [1, 2, 3]}, index=["A", "B", "C"]) + msg = r"Row\(s\) or Column\(s\) \['A', 'B'\] do not exist" + with pytest.raises(KeyError, match=msg): + df.transform({"A": lambda x: x + 2, "B": lambda x: x * 2}, axis=0) + + msg = r"Row\(s\) or Column\(s\) \['bar', 'foo'\] do not exist" + with pytest.raises(KeyError, match=msg): + df.transform({"foo": lambda x: x + 2, "bar": lambda x: x * 2}, axis=1) From 674e6e0359a4a8b333d21fd12e48caa94903e306 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 7 May 2024 13:59:51 +0800 Subject: [PATCH 4/6] Improve test name --- pandas/tests/apply/test_invalid_arg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 8f9c3bb1336b2..d913d658fda22 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -361,7 +361,7 @@ def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper): obj.transform(op) -def test_transform_axis_raises(): +def test_transform_missing_labels_raises(): # GH 58474 df = DataFrame({"foo": [2, 4, 6], "bar": [1, 2, 3]}, index=["A", "B", "C"]) msg = r"Row\(s\) or Column\(s\) \['A', 'B'\] do not exist" From 7d5cc86a7a1529ceded9f91db96c2b99187bb121 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Fri, 10 May 2024 11:39:53 +0800 Subject: [PATCH 5/6] Improve error msg --- pandas/core/apply.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index a82aef1ab111e..32e8aea7ea8ab 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -628,7 +628,8 @@ def normalize_dictlike_arg( cols = Index(list(func.keys())).difference(obj.columns, sort=True) if len(cols) > 0: - raise KeyError(f"Row(s) or Column(s) {list(cols)} do not exist") + # GH 58474 + raise KeyError(f"Label(s) {list(cols)} do not exist") aggregator_types = (list, tuple, dict) From 1e858597b19f21612ba615d9228364f0e0d38320 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Fri, 10 May 2024 14:03:10 +0800 Subject: [PATCH 6/6] Improve msg in test --- pandas/tests/apply/test_invalid_arg.py | 10 +++++----- pandas/tests/groupby/aggregate/test_aggregate.py | 5 ++--- pandas/tests/groupby/aggregate/test_other.py | 4 ++-- pandas/tests/resample/test_resample_api.py | 10 +++++----- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index d913d658fda22..3137d3ff50954 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -118,15 +118,15 @@ def test_dict_nested_renaming_depr(method): def test_missing_column(method, func): # GH 40004 obj = DataFrame({"A": [1]}) - match = re.escape("Column(s) ['B'] do not exist") - with pytest.raises(KeyError, match=match): + msg = r"Label\(s\) \['B'\] do not exist" + with pytest.raises(KeyError, match=msg): getattr(obj, method)(func) def test_transform_mixed_column_name_dtypes(): # GH39025 df = DataFrame({"a": ["1"]}) - msg = r"Column\(s\) \[1, 'b'\] do not exist" + msg = r"Label\(s\) \[1, 'b'\] do not exist" with pytest.raises(KeyError, match=msg): df.transform({"a": int, 1: str, "b": int}) @@ -364,10 +364,10 @@ def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper): def test_transform_missing_labels_raises(): # GH 58474 df = DataFrame({"foo": [2, 4, 6], "bar": [1, 2, 3]}, index=["A", "B", "C"]) - msg = r"Row\(s\) or Column\(s\) \['A', 'B'\] do not exist" + msg = r"Label\(s\) \['A', 'B'\] do not exist" with pytest.raises(KeyError, match=msg): df.transform({"A": lambda x: x + 2, "B": lambda x: x * 2}, axis=0) - msg = r"Row\(s\) or Column\(s\) \['bar', 'foo'\] do not exist" + msg = r"Label\(s\) \['bar', 'foo'\] do not exist" with pytest.raises(KeyError, match=msg): df.transform({"foo": lambda x: x + 2, "bar": lambda x: x * 2}, axis=1) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 2b9df1b7079da..3362d6209af6d 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -5,7 +5,6 @@ import datetime import functools from functools import partial -import re import numpy as np import pytest @@ -816,8 +815,8 @@ def test_agg_relabel_other_raises(self): def test_missing_raises(self): df = DataFrame({"A": [0, 1], "B": [1, 2]}) - match = re.escape("Column(s) ['C'] do not exist") - with pytest.raises(KeyError, match=match): + msg = r"Label\(s\) \['C'\] do not exist" + with pytest.raises(KeyError, match=msg): df.groupby("A").agg(c=("C", "sum")) def test_agg_namedtuple(self): diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index 12f99e3cf7a63..78f2917e9a057 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -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"Column\(s\) \['r', 'r2'\] do not exist" + msg = r"Label\(s\) \['r', 'r2'\] do not exist" with pytest.raises(KeyError, match=msg): grouped[["D", "C"]].agg({"r": "sum", "r2": "mean"}) @@ -224,7 +224,7 @@ def test_agg_dict_renaming_deprecation(): {"B": {"foo": ["sum", "max"]}, "C": {"bar": ["count", "min"]}} ) - msg = r"Column\(s\) \['ma'\] do not exist" + msg = r"Label\(s\) \['ma'\] do not exist" with pytest.raises(KeyError, match=msg): df.groupby("A")[["B", "C"]].agg({"ma": "max"}) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index a77097fd5ce61..bf1f6bd34b171 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -328,7 +328,7 @@ def test_agg_consistency(): r = df.resample("3min") - msg = r"Column\(s\) \['r1', 'r2'\] do not exist" + msg = r"Label\(s\) \['r1', 'r2'\] do not exist" with pytest.raises(KeyError, match=msg): r.agg({"r1": "mean", "r2": "sum"}) @@ -343,7 +343,7 @@ def test_agg_consistency_int_str_column_mix(): r = df.resample("3min") - msg = r"Column\(s\) \[2, 'b'\] do not exist" + msg = r"Label\(s\) \[2, 'b'\] do not exist" with pytest.raises(KeyError, match=msg): r.agg({2: "mean", "b": "sum"}) @@ -534,7 +534,7 @@ def test_agg_with_lambda(cases, agg): ], ) def test_agg_no_column(cases, agg): - msg = r"Column\(s\) \['result1', 'result2'\] do not exist" + msg = r"Label\(s\) \['result1', 'result2'\] do not exist" with pytest.raises(KeyError, match=msg): cases[["A", "B"]].agg(**agg) @@ -582,7 +582,7 @@ def test_agg_specificationerror_series(cases, agg): def test_agg_specificationerror_invalid_names(cases): # errors # invalid names in the agg specification - msg = r"Column\(s\) \['B'\] do not exist" + msg = r"Label\(s\) \['B'\] do not exist" with pytest.raises(KeyError, match=msg): cases[["A"]].agg({"A": ["sum", "std"], "B": ["mean", "std"]}) @@ -631,7 +631,7 @@ def test_try_aggregate_non_existing_column(): df = DataFrame(data).set_index("dt") # Error as we don't have 'z' column - msg = r"Column\(s\) \['z'\] do not exist" + msg = r"Label\(s\) \['z'\] do not exist" with pytest.raises(KeyError, match=msg): df.resample("30min").agg({"x": ["mean"], "y": ["median"], "z": ["sum"]})