Repository: pandas-dev/pandas
Pull request: TST: Test to verify behavior of groupby.std with no numeric columns and numeric_only=True
- [x] closes #51080
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Problem statement:
BUG: groupby.std with no numeric columns and numeric_only=True raises
```
df = pd.DataFrame({'a': list('xyz'), 'b': list('def')})
gb = df.groupby('a')
print(gb.std(numeric_only=True))
```

raises `TypeError: All columns were dropped in grouped_reduce`. Instead, it should return an empty frame with index consisting of the groups.

Hints from issue discussion:
I haven't verified this, but it looks like this was fixed in

```
commit bcb6c1adf5f18465389dad781436626dddd6c64b
Author: jbrockmendel
Date:   Tue Jan 31 12:03:29 2023 -0800

    CLN: assorted (#51078)
```

cc @jbrockmendel. Could use tests.
take

Commit message: Merge remote-tracking branch 'upstream/main' into groupby-std-numeric-only-tests

Patch to review:
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index 55e14bc11246b..e5f0cf6b1dbae 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -1915,7 +1915,7 @@ def std(
             .. versionadded:: 1.4.0
 
         numeric_only : bool, default False
-            Include only `float`, `int` or `boolean` data.
+            Include only `float`, `int` or `boolean` columns.
 
             .. versionadded:: 1.5.0
 
@@ -1998,7 +1998,7 @@ def var(
             .. versionadded:: 1.4.0
 
         numeric_only : bool, default False
-            Include only `float`, `int` or `boolean` data.
+            Include only `float`, `int` or `boolean` columns.
 
             .. versionadded:: 1.5.0
 
@@ -2167,7 +2167,7 @@ def sem(self, ddof: int = 1, numeric_only: bool = False):
             Degrees of freedom.
 
         numeric_only : bool, default False
-            Include only `float`, `int` or `boolean` data.
+            Include only `float`, `int` or `boolean` columns.
 
             .. versionadded:: 1.5.0
 
@@ -3093,7 +3093,7 @@ def quantile(
         interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
             Method to use when the desired quantile falls between two points.
         numeric_only : bool, default False
-            Include only `float`, `int` or `boolean` data.
+            Include only `float`, `int` or `boolean` columns.
 
             .. versionadded:: 1.5.0
 
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
index 97e88a8545aa5..37ef7e088d680 100644
--- a/pandas/tests/groupby/test_groupby.py
+++ b/pandas/tests/groupby/test_groupby.py
@@ -2843,3 +2843,22 @@ 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)
+
+
+def test_groupby_numeric_only_std_no_result():
+    # GH 51080
+    dicts_non_numeric = [{"a": "foo", "b": "bar"}, {"a": "car", "b": "dar"}]
+    df = DataFrame(dicts_non_numeric)
+    dfgb = df.groupby("a")
+    result = dfgb.std(numeric_only=True)
+
+    assert result.empty
+
+
+def test_groupby_std_raises_error():
+    # GH 51080
+    dicts_non_numeric = [{"a": "foo", "b": "bar"}, {"a": "car", "b": "dar"}]
+    df = DataFrame(dicts_non_numeric)
+    dfgb = df.groupby("a")
+    with pytest.raises(ValueError, match="could not convert string to float: 'bar'"):
+        dfgb.std()


Code review comments:
diff_hunk: @@ -1915,7 +1915,7 @@ def std(
             .. versionadded:: 1.4.0
 
         numeric_only : bool, default False
-            Include only `float`, `int` or `boolean` data.
+            Include only `float`, `int` or `boolean` columns.
path: pandas/core/groupby/groupby.py
text: @user1:
I believe `data` is used here because this docstring is shared by DataFrameGroupBy and SeriesGroupBy; the latter does not have columns. I think this should be reverted.

@author:
Makes sense. I have reverted these changes. But should't we have separate docstrings for DataFrameGroupBy and SeriesGroupBy for more clarity?

@user1:
We could template these docstrings or just separate them out entirely; but that should be a separate PR.