Skip to content

TST: Test to verify behavior of groupby.std with no numeric columns and numeric_only=True #51761

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
18 changes: 18 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2873,3 +2873,21 @@ def test_obj_with_exclusions_duplicate_columns():
result = gb._obj_with_exclusions
expected = df.take([0, 2, 3], axis=1)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("numeric_only", [True, False])
def test_groupby_numeric_only_std_no_result(numeric_only):
# GH 51080
dicts_non_numeric = [{"a": "foo", "b": "bar"}, {"a": "car", "b": "dar"}]
df = DataFrame(dicts_non_numeric)
dfgb = df.groupby("a", as_index=False, sort=False)

if numeric_only:
result = dfgb.std(numeric_only=True)
expected_df = DataFrame(["foo", "car"], columns=["a"])
tm.assert_frame_equal(result, expected_df)
else:
with pytest.raises(
ValueError, match="could not convert string to float: 'bar'"
):
dfgb.std(numeric_only=numeric_only)