Repository: pandas-dev/pandas

Issue:
resample().apply not returning multiple columns like groupby(pd.Timegrouper()).apply
#### Code Sample, a copy-pastable example if possible

```python

In [3]: date = pd.date_range('1-1-2015', '12-31-15', freq='D')

In [4]: df = pd.DataFrame(data={'col1':np.random.rand(len(date))}, index=date)

In [5]: def calc(x):
                s = pd.Series([1,2], index=['a', 'b'])
                return s

In [6]: df.resample('M').apply(calc)
Out[6]:
            col1
2015-01-31   NaN
2015-02-28   NaN
2015-03-31   NaN
2015-04-30   NaN
2015-05-31   NaN
2015-06-30   NaN
2015-07-31   NaN
2015-08-31   NaN
2015-09-30   NaN
2015-10-31   NaN
2015-11-30   NaN
2015-12-31   NaN

In [7]: df.groupby(pd.TimeGrouper('M')).apply(calc)
Out[7]:
            a  b
2015-01-31  1  2
2015-02-28  1  2
2015-03-31  1  2
2015-04-30  1  2
2015-05-31  1  2
2015-06-30  1  2
2015-07-31  1  2
2015-08-31  1  2
2015-09-30  1  2
2015-10-31  1  2
2015-11-30  1  2
2015-12-31  1  2

```
#### Problem description

It is my understanding that ``resample`` with `apply` should work very similarly as ``groupby(pd.Timegrouper)`` with  ``apply``. In a more complex example I was trying to return many aggregated results that are calculated with several columns. It seems `resample` with `apply` is unable to return anything but a Series that has the same index as the calling DataFrame columns.

#### Expected Output

Should look exactly like the output from ``df.groupby(pd.TimeGrouper('M')).apply(calc)``

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.19.2
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.12.0
scipy: 0.18.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.3.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.6.1
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.7
lxml: 3.6.0
bs4: 4.4.1
html5lib: 0.9999999
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: 0.7.5.None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: 0.2.1

</details>



Discussion:
these should be the same. welcome to have a look.
@jreback Line 330 in tseries/resample.py has `apply = aggregate` so they are exactly the same thing. `apply` was never implemented.
you have to trace deeper

there is a lot of indirection
@jreback 
The source of an error is a call of [aggregate](https://github.com/pandas-dev/pandas/blob/master/pandas/core/resample.py#L397-L403) method in `core/resample.py:Resampler.apply`. `apply` method is called when `aggregate` is failing.
But in our example, `aggregate` returns the result
```
            col1
2015-01-31   NaN
2015-02-28   NaN
2015-03-31   NaN
2015-04-30   NaN
2015-05-31   NaN
2015-06-30   NaN
2015-07-31   NaN
2015-08-31   NaN
2015-09-30   NaN
2015-10-31   NaN
2015-11-30   NaN
2015-12-31   NaN
```
And `apply` method is not called.

A possible solution would be the check if applied function is reducing or not, instead of calling of `aggregate` directly.

What do you think?
sure u are welcome to propose that as a soln

Resolving patch:
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -395,7 +395,11 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
             grouped = PanelGroupBy(obj, grouper=grouper, axis=self.axis)
 
         try:
-            result = grouped.aggregate(how, *args, **kwargs)
+            if isinstance(obj, ABCDataFrame) and compat.callable(how):
+                # Check if the function is reducing or not.
+                result = grouped._aggregate_item_by_item(how, *args, **kwargs)
+            else:
+                result = grouped.aggregate(how, *args, **kwargs)
         except Exception:
 
             # we have a non-reducing function


Test patch:
diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py
--- a/pandas/tests/test_resample.py
+++ b/pandas/tests/test_resample.py
@@ -3103,6 +3103,26 @@ def f(x):
         result = g.apply(f)
         assert_frame_equal(result, expected)
 
+    def test_apply_with_mutated_index(self):
+        # GH 15169
+        index = pd.date_range('1-1-2015', '12-31-15', freq='D')
+        df = pd.DataFrame(data={'col1': np.random.rand(len(index))},
+                          index=index)
+
+        def f(x):
+            s = pd.Series([1, 2], index=['a', 'b'])
+            return s
+
+        expected = df.groupby(pd.Grouper(freq='M')).apply(f)
+
+        result = df.resample('M').apply(f)
+        assert_frame_equal(result, expected)
+
+        # A case for series
+        expected = df['col1'].groupby(pd.Grouper(freq='M')).apply(f)
+        result = df['col1'].resample('M').apply(f)
+        assert_series_equal(result, expected)
+
     def test_resample_groupby_with_label(self):
         # GH 13235
         index = date_range('2000-01-01', freq='2D', periods=5)