Bug groupby idxmin
State: closed

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


Labels:
name: Bug
name: Groupby

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

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

```diff
@@            Coverage Diff             @@
##           master   #25531      +/-   ##
==========================================
+ Coverage   91.75%   91.75%   +<.01%     
==========================================
  Files         173      173              
  Lines       52960    52962       +2     
==========================================
+ Hits        48595    48597       +2     
  Misses       4365     4365
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.33% <100%> (ø)` | :arrow_up: |
| #single | `41.71% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25531?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/generic.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9nZW5lcmljLnB5) | `87.03% <100%> (+0.03%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25531?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/25531?src=pr&el=footer). Last update [bd49d2f...538f8f5](https://codecov.io/gh/pandas-dev/pandas/pull/25531?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

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

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

```diff
@@            Coverage Diff             @@
##           master   #25531      +/-   ##
==========================================
+ Coverage   91.26%   91.47%   +0.21%     
==========================================
  Files         172      173       +1     
  Lines       52965    52875      -90     
==========================================
+ Hits        48337    48367      +30     
+ Misses       4628     4508     -120
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.03% <100%> (+0.2%)` | :arrow_up: |
| #single | `41.82% <12.5%> (+0.07%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25531?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/generic.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9nZW5lcmljLnB5) | `87.1% <100%> (+0.1%)` | :arrow_up: |
| [pandas/core/groupby/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9iYXNlLnB5) | `91.83% <100%> (ø)` | :arrow_up: |
| [pandas/io/feather\_format.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2ZlYXRoZXJfZm9ybWF0LnB5) | `89.47% <0%> (-0.27%)` | :arrow_down: |
| [pandas/core/computation/engines.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tcHV0YXRpb24vZW5naW5lcy5weQ==) | `88.7% <0%> (-0.18%)` | :arrow_down: |
| [pandas/plotting/\_tools.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL190b29scy5weQ==) | `78.65% <0%> (-0.12%)` | :arrow_down: |
| [pandas/io/packers.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhY2tlcnMucHk=) | `88.11% <0%> (-0.11%)` | :arrow_down: |
| [pandas/core/panel.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcGFuZWwucHk=) | `35.62% <0%> (-0.1%)` | :arrow_down: |
| [pandas/io/excel/\_xlrd.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2V4Y2VsL194bHJkLnB5) | `93.93% <0%> (-0.1%)` | :arrow_down: |
| [pandas/io/formats/printing.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvcHJpbnRpbmcucHk=) | `85.34% <0%> (-0.08%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `89.3% <0%> (-0.05%)` | :arrow_down: |
| ... and [55 more](https://codecov.io/gh/pandas-dev/pandas/pull/25531/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25531?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/25531?src=pr&el=footer). Last update [8e54e55...28b5ab2](https://codecov.io/gh/pandas-dev/pandas/pull/25531?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

can you update
> can you update

I do not know, how to deal with one of your previous comment that says "this should already be done in _try_cast" . I didn't understand it clearly. I do not know if it should be already there (without my changes) and I should only debug it, or if I should move the logic (```if self._transform_should_cast(func)```)  into ```_try_cast``` function (and potentially rename ```_transform_should_cast``` to generic name ```_should_cast``` as @WillAyd reqested)...
Thanks in advance.
> should move the logic (`if self._transform_should_cast(func)`) into `_try_cast`

I think this would be most logical
> > should move the logic (`if self._transform_should_cast(func)`) into `_try_cast`
> 
> I think this would be most logical

Ouuu, I tried to do it but I found another problem - in `_try_cast` you do not have `func` parameter. So  you are not able to decide if cast or not inside `_try_cast`... 
@rbenes can you have a look thru groupby / datetime issues, I am almost sure this is a duplicate (which this PR would solve)
> @rbenes can you have a look thru groupby / datetime issues, I am almost sure this is a duplicate (which this PR would solve)

I found these, but none of them is very close to this PR:

already closed - https://github.com/pandas-dev/pandas/issues/19200 
"similar", but not solved in my PR - https://github.com/pandas-dev/pandas/issues/17035
fixed even on master - https://github.com/pandas-dev/pandas/issues/15306
@rbenes the last of those issues still persists on me for master - are you sure this doesn't solve that as well?
@rbenes yeah this looks ok, but I agree here that #15306 *should* be solved by this.
> @rbenes yeah this looks ok, but I agree here that #15306 _should_ be solved by this.

@WillAyd @jreback yes guys, you both were right.  
here is output:

master
```
(pandas) ➜  pandas git:(master) ✗ python my_test.py
                price  week_id
2011-01-01  10.050562       52
2011-01-02  10.550513       52
2011-01-03   9.554604        1
2011-01-04  10.248203        1
2011-01-05   9.829901        1
2011-01-06   8.245324        1
2011-01-07   7.597617        1
2011-01-08   8.196192        1
2011-01-09   8.528442        1
2011-01-10   7.380966        2
2011-01-11   7.999635        2
2011-01-12   7.911648        2
2011-01-13   8.336721        2
2011-01-14   8.668974        2
2011-01-15   7.512158        2

2011-01-01    1.293926e+18
2011-01-02    1.293926e+18
2011-01-03    1.294099e+18
2011-01-04    1.294099e+18
2011-01-05    1.294099e+18
2011-01-06    1.294099e+18
2011-01-07    1.294099e+18
2011-01-08    1.294099e+18
2011-01-09    1.294099e+18
2011-01-10    1.294963e+18
2011-01-11    1.294963e+18
2011-01-12    1.294963e+18
2011-01-13    1.294963e+18
2011-01-14    1.294963e+18
2011-01-15    1.294963e+18
Freq: D, Name: price, dtype: float64
week_id
1    2011-01-04
2    2011-01-14
52   2011-01-02
```

after PR
```
(pandas) ➜  pandas git:(bug_groupby_idxmin) ✗ python my_test.py
                price  week_id
2011-01-01  10.050562       52
2011-01-02  10.550513       52
2011-01-03   9.554604        1
2011-01-04  10.248203        1
2011-01-05   9.829901        1
2011-01-06   8.245324        1
2011-01-07   7.597617        1
2011-01-08   8.196192        1
2011-01-09   8.528442        1
2011-01-10   7.380966        2
2011-01-11   7.999635        2
2011-01-12   7.911648        2
2011-01-13   8.336721        2
2011-01-14   8.668974        2
2011-01-15   7.512158        2

2011-01-01   2011-01-02
2011-01-02   2011-01-02
2011-01-03   2011-01-04
2011-01-04   2011-01-04
2011-01-05   2011-01-04
2011-01-06   2011-01-04
2011-01-07   2011-01-04
2011-01-08   2011-01-04
2011-01-09   2011-01-04
2011-01-10   2011-01-14
2011-01-11   2011-01-14
2011-01-12   2011-01-14
2011-01-13   2011-01-14
2011-01-14   2011-01-14
2011-01-15   2011-01-14
Freq: D, Name: price, dtype: datetime64[ns]
week_id
1    2011-01-04
2    2011-01-14
52   2011-01-02
```
@rbenes great. can you add that as an additional test (and add to the whatsnew note that issue)
thanks @rbenes