|
11 | 11 | concat, |
12 | 12 | ) |
13 | 13 | import pandas._testing as tm |
14 | | -from pandas.core.base import DataError |
15 | 14 |
|
16 | 15 |
|
17 | 16 | def test_rank_apply(): |
@@ -462,21 +461,32 @@ def test_rank_avg_even_vals(dtype, upper): |
462 | 461 | tm.assert_frame_equal(result, exp_df) |
463 | 462 |
|
464 | 463 |
|
465 | | -@pytest.mark.xfail(reason="Works now, needs tests") |
466 | 464 | @pytest.mark.parametrize("ties_method", ["average", "min", "max", "first", "dense"]) |
467 | 465 | @pytest.mark.parametrize("ascending", [True, False]) |
468 | 466 | @pytest.mark.parametrize("na_option", ["keep", "top", "bottom"]) |
469 | 467 | @pytest.mark.parametrize("pct", [True, False]) |
470 | 468 | @pytest.mark.parametrize( |
471 | 469 | "vals", [["bar", "bar", "foo", "bar", "baz"], ["bar", np.nan, "foo", np.nan, "baz"]] |
472 | 470 | ) |
473 | | -def test_rank_object_raises(ties_method, ascending, na_option, pct, vals): |
| 471 | +def test_rank_object_dtype(ties_method, ascending, na_option, pct, vals): |
474 | 472 | df = DataFrame({"key": ["foo"] * 5, "val": vals}) |
| 473 | + mask = df["val"].isna() |
475 | 474 |
|
476 | | - with pytest.raises(DataError, match="No numeric types to aggregate"): |
477 | | - df.groupby("key").rank( |
478 | | - method=ties_method, ascending=ascending, na_option=na_option, pct=pct |
479 | | - ) |
| 475 | + gb = df.groupby("key") |
| 476 | + res = gb.rank(method=ties_method, ascending=ascending, na_option=na_option, pct=pct) |
| 477 | + |
| 478 | + # construct our expected by using numeric values with the same ordering |
| 479 | + if mask.any(): |
| 480 | + df2 = DataFrame({"key": ["foo"] * 5, "val": [0, np.nan, 2, np.nan, 1]}) |
| 481 | + else: |
| 482 | + df2 = DataFrame({"key": ["foo"] * 5, "val": [0, 0, 2, 0, 1]}) |
| 483 | + |
| 484 | + gb2 = df2.groupby("key") |
| 485 | + alt = gb2.rank( |
| 486 | + method=ties_method, ascending=ascending, na_option=na_option, pct=pct |
| 487 | + ) |
| 488 | + |
| 489 | + tm.assert_frame_equal(res, alt) |
480 | 490 |
|
481 | 491 |
|
482 | 492 | @pytest.mark.parametrize("na_option", [True, "bad", 1]) |
|
0 commit comments