Skip to content

Commit 72c59cf

Browse files
committed
update exception messages
1 parent 75eddea commit 72c59cf

File tree

5 files changed

+12
-37
lines changed

5 files changed

+12
-37
lines changed

pandas/core/groupby/groupby.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -4283,11 +4283,7 @@ def quantile(
42834283
starts, ends = lib.generate_slices(splitter._slabels, splitter.ngroups)
42844284

42854285
def pre_processor(vals: ArrayLike) -> tuple[np.ndarray, DtypeObj | None]:
4286-
if is_object_dtype(vals.dtype):
4287-
raise TypeError(
4288-
"'quantile' cannot be performed against 'object' dtypes!"
4289-
)
4290-
elif isinstance(vals.dtype, StringDtype):
4286+
if isinstance(vals.dtype, StringDtype) or is_object_dtype(vals.dtype):
42914287
raise TypeError(
42924288
f"dtype '{vals.dtype}' does not support operation 'quantile'"
42934289
)

pandas/tests/groupby/methods/test_quantile.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ def test_groupby_quantile_nullable_array(values, q):
241241
tm.assert_series_equal(result, expected)
242242

243243

244-
# @pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
245244
@pytest.mark.parametrize("q", [0.5, [0.0, 0.5, 1.0]])
246245
@pytest.mark.parametrize("numeric_only", [True, False])
247246
def test_groupby_quantile_raises_on_invalid_dtype(q, numeric_only, using_infer_string):
@@ -251,9 +250,7 @@ def test_groupby_quantile_raises_on_invalid_dtype(q, numeric_only, using_infer_s
251250
expected = df.groupby("a")[["b"]].quantile(q)
252251
tm.assert_frame_equal(result, expected)
253252
else:
254-
msg = "'quantile' cannot be performed against 'object' dtypes!"
255-
if using_infer_string:
256-
msg = "dtype 'str' does not support operation 'quantile'"
253+
msg = "dtype '.*' does not support operation 'quantile'"
257254
with pytest.raises(TypeError, match=msg):
258255
df.groupby("a").quantile(q, numeric_only=numeric_only)
259256

pandas/tests/groupby/test_groupby.py

+4-18
Original file line numberDiff line numberDiff line change
@@ -983,18 +983,11 @@ def test_groupby_with_hier_columns():
983983
tm.assert_index_equal(result.columns, df.columns[:-1])
984984

985985

986-
def test_grouping_ndarray(df, using_infer_string):
986+
def test_grouping_ndarray(df):
987+
df = df.astype({"A": object, "B": object})
987988
grouped = df.groupby(df["A"].values)
988989
grouped2 = df.groupby(df["A"].rename(None))
989990

990-
if using_infer_string:
991-
msg = "dtype 'str' does not support operation 'sum'"
992-
with pytest.raises(TypeError, match=msg):
993-
grouped.sum()
994-
with pytest.raises(TypeError, match=msg):
995-
grouped2.sum()
996-
return
997-
998991
result = grouped.sum()
999992
expected = grouped2.sum()
1000993
tm.assert_frame_equal(result, expected)
@@ -1495,18 +1488,11 @@ def f(group):
14951488
assert names == expected_names
14961489

14971490

1498-
def test_no_dummy_key_names(df, using_infer_string):
1491+
def test_no_dummy_key_names(df):
14991492
# see gh-1291
1493+
df = df.astype({"A": object, "B": object})
15001494
gb = df.groupby(df["A"].values)
15011495
gb2 = df.groupby([df["A"].values, df["B"].values])
1502-
if using_infer_string:
1503-
msg = "dtype 'str' does not support operation 'sum'"
1504-
with pytest.raises(TypeError, match=msg):
1505-
gb.sum()
1506-
with pytest.raises(TypeError, match=msg):
1507-
gb2.sum()
1508-
return
1509-
15101496
result = gb.sum()
15111497
assert result.index.name is None
15121498

pandas/tests/groupby/test_numeric_only.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ def test_numeric_only(kernel, has_arg, numeric_only, keys):
299299
re.escape(f"agg function failed [how->{kernel},dtype->object]"),
300300
]
301301
)
302-
if kernel == "idxmin":
302+
if kernel == "quantile":
303+
msg = "dtype 'object' does not support operation 'quantile'"
304+
elif kernel == "idxmin":
303305
msg = "'<' not supported between instances of 'type' and 'type'"
304306
elif kernel == "idxmax":
305307
msg = "'>' not supported between instances of 'type' and 'type'"
@@ -379,7 +381,7 @@ def test_deprecate_numeric_only_series(dtype, groupby_func, request):
379381
# that succeed should not be allowed to fail (without deprecation, at least)
380382
if groupby_func in fails_on_numeric_object and dtype is object:
381383
if groupby_func == "quantile":
382-
msg = "cannot be performed against 'object' dtypes"
384+
msg = "dtype 'object' does not support operation 'quantile'"
383385
else:
384386
msg = "is not supported for object dtype"
385387
with pytest.raises(TypeError, match=msg):

pandas/tests/groupby/test_raises.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,12 @@ def test_groupby_raises_string(
204204
elif groupby_func == "pct_change" and df["d"].dtype.storage == "pyarrow":
205205
# This doesn't go through EA._groupby_op so the message isn't controlled
206206
# there.
207-
import pyarrow as pa
208-
209-
klass = pa.lib.ArrowNotImplementedError
210-
msg = "Function 'divide' has no kernel matching input types"
207+
msg = "operation 'truediv' not supported for dtype 'str' with dtype 'str'"
211208
elif groupby_func == "diff" and df["d"].dtype.storage == "pyarrow":
212209
# This doesn't go through EA._groupby_op so the message isn't controlled
213210
# there.
214-
import pyarrow as pa
211+
msg = "operation 'sub' not supported for dtype 'str' with dtype 'str'"
215212

216-
# TODO(infer_string): avoid bubbling up pyarrow exceptions
217-
klass = pa.lib.ArrowNotImplementedError
218-
msg = "Function 'subtract_checked' has no kernel matching input types"
219213
elif groupby_func in ["cummin", "cummax"]:
220214
msg = msg.replace("object", "str")
221215
elif groupby_func == "corrwith":

0 commit comments

Comments
 (0)