Repository: pandas-dev/pandas

Issue:
GroupBy OHLC Error When Used in .agg with Other Functions
#### Code Sample, a copy-pastable example if possible

```python-traceback
In [12]: np.random.seed(1234)
In [13]: df = pd.DataFrame(np.random.rand(10, 1), 
    index=pd.date_range(start='2018-01-01', freq='5H', periods=10))

# This call works fine
In [14]: df.groupby(pd.Grouper(freq='D')).agg(['ohlc', 'sum'])
Out[14]: 
                   0                                        
                ohlc                                     sum
                open      high       low     close         0
2018-01-01  0.191519  0.785359  0.191519  0.779976  2.816690
2018-01-02  0.272593  0.958139  0.272593  0.875933  3.185001

# Switching the order of ops in agg doesn't work
In [16]: df.groupby(pd.Grouper(freq='D')).agg(['sum', 'ohlc'])
ValueError: no results
```

I noticed that `ohlc` gets special treatment in some of the groupby functions to assign `names`. That occurs while iterating over `self._iterate_slices` however and therefore *may* be dependent upon the order of evaluation (have not confirmed in looking at code)

<details>

INSTALLED VERSIONS
------------------
commit: 7cd2679982138e1e7f9f1a759827647e6e521d43
python: 3.6.4.final.0
python-bits: 64
OS: Darwin
OS-release: 17.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.0.dev0+205.g7cd267998
pytest: 3.4.1
pip: 10.0.1
setuptools: 38.5.1
Cython: 0.27.3
numpy: 1.14.1
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.7.0
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.2
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.5
pymysql: 0.8.0
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: 0.1.3
fastparquet: 0.1.4
pandas_gbq: 0.4.1
pandas_datareader: None
gcsfs: None

</details>


Discussion:

Resolving patch:
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -3549,13 +3549,11 @@ def _aggregate_multiple_funcs(self, arg, _level):
                 obj._selection = name
             results[name] = obj.aggregate(func)
 
-        if isinstance(list(compat.itervalues(results))[0],
-                      DataFrame):
-
+        if any(isinstance(x, DataFrame) for x in compat.itervalues(results)):
             # let higher level handle
             if _level:
                 return results
-            return list(compat.itervalues(results))[0]
+
         return DataFrame(results, columns=columns)
 
     def _wrap_output(self, output, index, names=None):


Test patch:
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
--- a/pandas/tests/groupby/test_groupby.py
+++ b/pandas/tests/groupby/test_groupby.py
@@ -1674,3 +1674,22 @@ def test_tuple_correct_keyerror():
                                                           [3, 4]]))
     with tm.assert_raises_regex(KeyError, "(7, 8)"):
         df.groupby((7, 8)).mean()
+
+
+def test_groupby_agg_ohlc_non_first():
+    # GH 21716
+    df = pd.DataFrame([[1], [1]], columns=['foo'],
+                      index=pd.date_range('2018-01-01', periods=2, freq='D'))
+
+    expected = pd.DataFrame([
+        [1, 1, 1, 1, 1],
+        [1, 1, 1, 1, 1]
+    ], columns=pd.MultiIndex.from_tuples((
+        ('foo', 'ohlc', 'open'), ('foo', 'ohlc', 'high'),
+        ('foo', 'ohlc', 'low'), ('foo', 'ohlc', 'close'),
+        ('foo', 'sum', 'foo'))), index=pd.date_range(
+            '2018-01-01', periods=2, freq='D'))
+
+    result = df.groupby(pd.Grouper(freq='D')).agg(['sum', 'ohlc'])
+
+    tm.assert_frame_equal(result, expected)