BUG: groupby with resample using on parameter errors when selecting column to apply function
State: closed

- [x] closes #17813
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Bug
name: Resample
description: resample method

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=h1) Report
> Merging [#19433](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/de39a1572fcf82071f3c0b5f22be1611222bdf41?src=pr&el=desc) will **decrease** coverage by `0.02%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/19433/graphs/tree.svg?token=eZ4WkYLtcO&src=pr&height=150&width=650)](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #19433      +/-   ##
==========================================
- Coverage   91.62%   91.59%   -0.03%     
==========================================
  Files         150      150              
  Lines       48714    48720       +6     
==========================================
- Hits        44633    44625       -8     
- Misses       4081     4095      +14
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.96% <100%> (-0.03%)` | :arrow_down: |
| #single | `41.75% <22.22%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby.py](https://codecov.io/gh/pandas-dev/pandas/pull/19433/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS5weQ==) | `92.17% <100%> (+0.02%)` | :arrow_up: |
| [pandas/plotting/\_converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/19433/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb252ZXJ0ZXIucHk=) | `65.22% <0%> (-1.74%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/19433/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `83.64% <0%> (-0.21%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=footer). Last update [de39a15...2f25d40](https://codecov.io/gh/pandas-dev/pandas/pull/19433?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@jreback 
try with something like this. your patch is just working around the issue and not fixing it at the source. its pretty deep code, but try here.

```
diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py
index 4c03f3bfa..6a2cd69eb 100644
--- a/pandas/core/groupby.py
+++ b/pandas/core/groupby.py
@@ -466,11 +466,15 @@ class Grouper(object):
                 "The Grouper cannot specify both a key and a level!")
 
         # the key must be a valid info item
-        if self.key is not None and not isinstance(obj, Series):
+        if self.key is not None:
             key = self.key
-            if key not in obj._info_axis:
-                raise KeyError("The grouper name {0} is not found".format(key))
-            ax = Index(obj[key], name=key)
+
+            if getattr(self.grouper, 'name', None) == key:
+                ax = self.grouper
+            else:
+                if key not in obj._info_axis:
+                    raise KeyError("The grouper name {0} is not found".format(key))
+                ax = Index(obj[key], name=key)
 
         else:
             ax = obj._get_axis(self.axis)
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
index 03b522abc..43391061d 100644
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -743,11 +743,6 @@ class _GroupByMixin(GroupByMixin):
 
             return x.apply(f, **kwargs)
 
-        # Patch the index in case we're applying after __getitem__. GH 17813
-        if self.groupby.key:
-            key = self.groupby.key
-            self._groupby.obj.index = Index(self.groupby.obj[key], key=key)
-
         result = self._groupby.apply(func)
         return self._wrap_result(result)
 
```
I applied your diff. The result, in this case, would be:
```
In [11]: result
Out[11]:
id  date
a   2016-01-01    2
    2016-01-03    0
    2016-01-05    0
b   2016-01-01    2
    2016-01-03    1
    2016-01-05    0
Name: data, dtype: int64
```

instead of:
```
In [10]: exp
Out[10]:
id  date
a   2016-01-01    2
b   2016-01-03    2
    2016-01-05    1
Name: data, dtype: int64
```

But I got your idea. The main challenge is that `self.grouper` is overriding [here](https://github.com/pandas-dev/pandas/blob/33a1202cd3e2dbc86b9b4adc1b54152c8cd67075/pandas/core/groupby.py#L511). Could you take a look at newest solution? @jreback 
@jreback 
thanks @discort