Skip to content

ZeroDivisionError when groupby rank with method="dense" and pct=True #23864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 3, 2018
Merged
Prev Previous commit
Next Next commit
Updated to have more test cases for better coverage
  • Loading branch information
Koustav-Samaddar committed Nov 24, 2018
commit 34047dac5c7eae2c252cde120ba8fa14aa3324c4
17 changes: 10 additions & 7 deletions pandas/tests/groupby/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,16 @@ def test_rank_empty_group():
tm.assert_frame_equal(result, expected)


def test_rank_zero_div():
@pytest.mark.parametrize("df_dicts", [
({"A": [1, 2], "B": [1, 1]}, {"B": [1.0, 1.0]}),
({"A": [1, 1, 2, 2], "B": [1, 2, 1, 2]}, {"B": [0.5, 1.0, 0.5, 1.0]}),
({"A": [1, 1, 2, 2], "B": [1, 2, 1, np.nan]}, {"B": [0.5, 1.0, 1.0, np.nan]}),
({"A": [1, 1, 2], "B": [1, 2, np.nan]}, {"B": [0.5, 1.0, np.nan]})
])
def test_rank_zero_div(df_dicts):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you passing a dict? just pass 2 arguments in the parameterize. this is very hard to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I understand this correctly, so I followed the same styling as a previous test in the same file.

I hope the changes are more readable!

# GH 23666
df = DataFrame({
"A": [1, 2],
"B": [1, 1]
})
df = DataFrame(df_dicts[0])

result = df.groupby("A").rank(pct=True, method="dense")
expected = DataFrame({"B": [1.0, 1.0]})
result = df.groupby("A").rank(method="dense", pct=True)
expected = DataFrame(df_dicts[1])
tm.assert_frame_equal(result, expected)