Skip to content

Commit 4c09f3b

Browse files
committed
BUG: quantile should drop non-numeric columns instead of raising
1 parent 5e21c72 commit 4c09f3b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pandas/core/groupby/groupby.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,9 @@ def _get_cythonized_result(
24112411
Function should return a tuple where the first element is the
24122412
values to be passed to Cython and the second element is an optional
24132413
type which the values should be converted to after being returned
2414-
by the Cython operation. Raises if `needs_values` is False.
2414+
by the Cython operation. This function is also responsible for
2415+
raising a TypeError if the values have an invalid type. Raises
2416+
if `needs_values` is False.
24152417
post_processing : function, default None
24162418
Function to be applied to result of Cython function. Should accept
24172419
an array of values as the first argument and type inferences as its
@@ -2443,6 +2445,7 @@ def _get_cythonized_result(
24432445
output: Dict[base.OutputKey, np.ndarray] = {}
24442446
base_func = getattr(libgroupby, how)
24452447

2448+
error_msg = ""
24462449
for idx, obj in enumerate(self._iterate_slices()):
24472450
name = obj.name
24482451
values = obj._values
@@ -2470,7 +2473,11 @@ def _get_cythonized_result(
24702473
if needs_values:
24712474
vals = values
24722475
if pre_processing:
2473-
vals, inferences = pre_processing(vals)
2476+
try:
2477+
vals, inferences = pre_processing(vals)
2478+
except TypeError as e:
2479+
error_msg = str(e)
2480+
continue
24742481
if needs_2d:
24752482
vals = vals.reshape((-1, 1))
24762483
vals = vals.astype(cython_dtype, copy=False)
@@ -2502,6 +2509,10 @@ def _get_cythonized_result(
25022509
key = base.OutputKey(label=name, position=idx)
25032510
output[key] = result
25042511

2512+
# Note: we don't raise on an frame/series with no rows
2513+
if len(output) == 0 and error_msg != "":
2514+
raise TypeError(error_msg)
2515+
25052516
if aggregate:
25062517
return self._wrap_aggregated_output(output)
25072518
else:

pandas/tests/groupby/test_quantile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,11 @@ def test_groupby_quantile_nullable_array(values, q):
232232

233233
expected = pd.Series(true_quantiles * 2, index=idx, name="b")
234234
tm.assert_series_equal(result, expected)
235+
236+
237+
@pytest.mark.parametrize("q", [0.5, [0.0, 0.5, 1.0]])
238+
def test_groupby_quantile_skips_invalid_dtype(q):
239+
df = pd.DataFrame({"a": [1], "b": [2.0], "c": ["x"]})
240+
result = df.groupby("a").quantile(0.5)
241+
expected = df.set_index("a")[["b"]]
242+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)