BUG: Group-by numeric type-coercion with datetime
State: closed

closes #14423
closes #15421
closes #15670

During a group-by/apply on a DataFrame, in the presence of one or more
DateTime-like columns, Pandas would incorrectly coerce the type of all
other columns to numeric.  E.g. a String column would be coerced to
numeric, producing NaNs.


Labels:
name: Bug
name: Groupby
name: Dtype Conversions
description: Unexpected or buggy dtype conversions

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


```diff
@@            Coverage Diff             @@
##           master   #15680      +/-   ##
==========================================
- Coverage   91.03%      91%   -0.03%     
==========================================
  Files         143      143              
  Lines       49392    49410      +18     
==========================================
+ Hits        44962    44967       +5     
- Misses       4430     4443      +13
```


| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/15680?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby.py](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...0a156745e39595fe125081282da2acc7f28cec13?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS5weQ==) | `94.98% <100%> (ø)` | :white_check_mark: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...0a156745e39595fe125081282da2acc7f28cec13?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :x: |
| [pandas/tools/merge.py](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...0a156745e39595fe125081282da2acc7f28cec13?src=pr&el=tree#diff-cGFuZGFzL3Rvb2xzL21lcmdlLnB5) | `93.68% <0%> (-0.27%)` | :x: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...0a156745e39595fe125081282da2acc7f28cec13?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.87% <0%> (-0.1%)` | :x: |
| [pandas/core/internals.py](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...0a156745e39595fe125081282da2acc7f28cec13?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW50ZXJuYWxzLnB5) | `93.59% <0%> (-0.06%)` | :x: |
| [pandas/core/reshape.py](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...0a156745e39595fe125081282da2acc7f28cec13?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcmVzaGFwZS5weQ==) | `99.27% <0%> (ø)` | :white_check_mark: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/15680?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/15680?src=pr&el=footer). Last update [05d70f4...e1ed104](https://codecov.io/gh/pandas-dev/pandas/compare/05d70f4e617a274813bdb02db69143b5554aa106...e1ed10401ab1255ad168afeaf744955a25069c59?el=footer&src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
pls add tests from these two (you can list all 3 issues in the whatsnew)

xref #14873 (boolean casts)
xref #14849 (datetime)

if this change doesn't fix these, pls lmk.
I tried adding the test for #14873 - my fix does not resolve this issue.  It's similar, but unrelated.   It seems to be this line in _python_agg_general:

`output[name] = self._try_cast(result, obj, numeric_only=True)`

that favours converting the result of the aggregate() back to the same type as the original series.
@gwpdt ok, we can treat that issue independently.
I also tried adding the test for #14849 - my fix does not resolve this issue either.

The issue with that test is that it is testing for preservation of dtype="object" for a column whose values are all numeric (and therefore the "soft coercion" to numeric type succeeds).

In this instance (in the absence of any non-numeric values in the series), even with my fix, it will still convert the dtype of the column from "object" to numeric.

I tried removing the conversion altogether.  That fixes #14849, but breaks another test (test_time_field_bug / #11324), so I will not commit that change.

I still think it is worth contributing my fix, since it improves upon the current situation.  A series with string values will not be converted to NaNs when doing groupby/apply on a DF with one or more datetime columns.

I will address other review feedback (re: "pd" import) and update the PR.
AFAICT this *only* resolved #15421 (w/o breaking anything else). so thats good.
#14423 I think *should* be fixable as well. want to give that a shot? (the example is not very good though).
Agreed - #15670 #15421 and #14423 are all fixed.
lgtm. merging shortly.
thanks!