Repository: pandas-dev/pandas

Issue:
SeriesGroupBy.transform cannot handle empty series
#### Code Sample, a copy-pastable example if possible

```python
d = pd.DataFrame({1: [], 2: []})
g = d.groupby(1)
g[2].transform(lambda x: x)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\python37\lib\site-packages\pandas\core\groupby\generic.py", line 945, in transform
    result = concat(results).sort_index()
  File "C:\python37\lib\site-packages\pandas\core\reshape\concat.py", line 228, in concat
    copy=copy, sort=sort)
  File "C:\python37\lib\site-packages\pandas\core\reshape\concat.py", line 262, in __init__
    raise ValueError('No objects to concatenate')
```

#### Problem description

Crashes on SeriesGroupby obj with zero length, which came from an empty dataframe. Would be nicer if pandas can handle this case without raising errors, by for example, just return an empty series. Thanks.

#### Expected Output

```
Series([], Name: 2, dtype: float64)
```

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.24.2
pytest: 4.4.1
pip: 19.0.3
setuptools: 41.0.1
Cython: None
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

</details>



Discussion:
I suppose that would make this consistent with apply and agg:

```python
>>> g[2].apply(lambda x: x)
Series([], Name: 2, dtype: float64)
>>> g[2].agg(lambda x: x)
Series([], Name: 2, dtype: float64)
```

If you want to take a look and have a simple way of making it work would take a PR
Related to #17093
Okay, thanks. I see a possible patch. Need to read over contribution guideline and set up a working env to create a PR.

Resolving patch:
diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py
--- a/pandas/core/groupby/generic.py
+++ b/pandas/core/groupby/generic.py
@@ -916,8 +916,12 @@ def transform(self, func, *args, **kwargs):
             s = klass(res, indexer)
             results.append(s)
 
-        from pandas.core.reshape.concat import concat
-        result = concat(results).sort_index()
+        # check for empty "results" to avoid concat ValueError
+        if results:
+            from pandas.core.reshape.concat import concat
+            result = concat(results).sort_index()
+        else:
+            result = Series()
 
         # we will only try to coerce the result type if
         # we have a numeric dtype, as these are *always* udfs


Test patch:
diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py
--- a/pandas/tests/groupby/test_grouping.py
+++ b/pandas/tests/groupby/test_grouping.py
@@ -552,6 +552,23 @@ def test_list_grouper_with_nat(self):
         expected = {pd.Timestamp('2011-01-01'): 365}
         tm.assert_dict_equal(result.groups, expected)
 
+    @pytest.mark.parametrize(
+        'func,expected',
+        [
+            ('transform', pd.Series(name=2, index=pd.RangeIndex(0, 0, 1))),
+            ('agg', pd.Series(name=2, index=pd.Float64Index([], name=1))),
+            ('apply', pd.Series(name=2, index=pd.Float64Index([], name=1))),
+        ])
+    def test_evaluate_with_empty_groups(self, func, expected):
+        # 26208
+        # test transform'ing empty groups
+        # (not testing other agg fns, because they return
+        # different index objects.
+        df = pd.DataFrame({1: [], 2: []})
+        g = df.groupby(1)
+        result = getattr(g[2], func)(lambda x: x)
+        assert_series_equal(result, expected)
+
 
 # get_group
 # --------------------------------