Skip to content

Commit 8009490

Browse files
jbrockmendelJulianWgs
authored andcommitted
TST: fix groupby xfails (pandas-dev#41387)
1 parent 8f93b2f commit 8009490

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

pandas/tests/groupby/test_groupby.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,7 @@ def test_pivot_table_values_key_error():
17381738
)
17391739
def test_empty_groupby(columns, keys, values, method, op, request):
17401740
# GH8093 & GH26411
1741+
override_dtype = None
17411742

17421743
if isinstance(values, Categorical) and len(keys) == 1 and method == "apply":
17431744
mark = pytest.mark.xfail(raises=TypeError, match="'str' object is not callable")
@@ -1784,12 +1785,9 @@ def test_empty_groupby(columns, keys, values, method, op, request):
17841785
and op in ["sum", "prod"]
17851786
and method != "apply"
17861787
):
1787-
mark = pytest.mark.xfail(
1788-
raises=AssertionError, match="(DataFrame|Series) are different"
1789-
)
1790-
request.node.add_marker(mark)
1788+
# We expect to get Int64 back for these
1789+
override_dtype = "Int64"
17911790

1792-
override_dtype = None
17931791
if isinstance(values[0], bool) and op in ("prod", "sum") and method != "apply":
17941792
# sum/product of bools is an integer
17951793
override_dtype = "int64"

pandas/tests/groupby/test_rank.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
concat,
1212
)
1313
import pandas._testing as tm
14-
from pandas.core.base import DataError
1514

1615

1716
def test_rank_apply():
@@ -462,21 +461,32 @@ def test_rank_avg_even_vals(dtype, upper):
462461
tm.assert_frame_equal(result, exp_df)
463462

464463

465-
@pytest.mark.xfail(reason="Works now, needs tests")
466464
@pytest.mark.parametrize("ties_method", ["average", "min", "max", "first", "dense"])
467465
@pytest.mark.parametrize("ascending", [True, False])
468466
@pytest.mark.parametrize("na_option", ["keep", "top", "bottom"])
469467
@pytest.mark.parametrize("pct", [True, False])
470468
@pytest.mark.parametrize(
471469
"vals", [["bar", "bar", "foo", "bar", "baz"], ["bar", np.nan, "foo", np.nan, "baz"]]
472470
)
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):
474472
df = DataFrame({"key": ["foo"] * 5, "val": vals})
473+
mask = df["val"].isna()
475474

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)
480490

481491

482492
@pytest.mark.parametrize("na_option", [True, "bad", 1])

0 commit comments

Comments
 (0)