ENH: support of pandas.DataFrame.hist for datetime data
State: closed

- [x] closes #32590
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

# Update

```
import numpy as np
from pandas import DataFrame, to_datetime
from datetime import timedelta

df = DataFrame(
    {
        "a": np.random.rand(10),
        "b": [timedelta(np.random.randn()) for _ in range(10)],
        "c": to_datetime(
            np.random.randint(
                1582800000000000000, 1583500000000000000, 10, dtype=np.int64
            )
        ),
        "d": to_datetime(
            np.random.randint(
                1582800000000000000, 1583500000000000000, 10, dtype=np.int64
            ),
            utc=True
        ),
    }
)

df.dtypes
a                float64
b        timedelta64[ns]
c         datetime64[ns]
d    datetime64[ns, UTC]
dtype: object

df.hist(xrot=90, figsize=(9, 6))
array([[<AxesSubplot:title={'center':'a'}>,
        <AxesSubplot:title={'center':'c'}>],
       [<AxesSubplot:title={'center':'d'}>, <AxesSubplot:>]], dtype=object)
```
![download](https://user-images.githubusercontent.com/21543236/93713409-3f0d8580-fb8e-11ea-934a-a6e302a7b14d.png)

# ~~WIP~~

Since there's no response for several days in https://github.com/pandas-dev/pandas/issues/32590, I decide to open this PR for further discussion.
There's two feasible options:

Plan A:
As is shown in the files changed, `data._get_numeric_data()` is modified to `data._get_numeric_or_datetime_data()`, and I will work on related tests as the next step.

Plan B, this may be a fotfix according to https://github.com/pandas-dev/pandas/issues/32590#issuecomment-687477764:
```
column_dt = data.select_dtypes(include='datetime64[ns]')
if len(column_dt) > 0:
    data[column_dt] = data[column_dt].astype(np.int64)
data = data._get_numeric_data()
if len(column_dt) > 0:
    data[column_dt] = data[column_dt].astype('datetime64[ns]')
```
Also, related tests will be modified.

Any comment / thought is welcomed.

Labels:
name: Visualization
description: plotting
name: Dtype Conversions
description: Unexpected or buggy dtype conversions

Comments:
Hello @onshek! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:







There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-10-04 12:59:16 UTC
Also need to update whatsnew note here?
@charlesdong1991 @jreback Two tests unexpectedly fails in https://github.com/pandas-dev/pandas/pull/36287/commits/35cccd649060b3428a9495c7485b2a93c283cdd0 (I think https://github.com/pandas-dev/pandas/pull/36287/commits/7569a319d6d30fc36a1e0156df095fdcbf6d9119 have the same problem even if it passed all checks), it's a bit more complicated than initially projected.
As mentioned in https://github.com/pandas-dev/pandas/pull/36287#discussion_r488632776, converting `datetimetz`, `datetime64` and `timedelta` into `np.int64` (or using map/dict, just like what https://github.com/pandas-dev/pandas/issues/32590#issuecomment-687477764 has discussed before) may be a simpler choice, which can leave all tests unchanged.
> Two tests unexpectedly fails in 35cccd6 (I think 7569a31 have the same problem even if it passed all checks), it's a bit more complicated than initially projected.

no worries, this flaky failure occurred before in other PRs, probably if you rebase and commit, it will be gone.

> As mentioned in #36287 (comment), converting datetimetz, datetime64 and timedelta into np.int64 (or using map/dict, just like what #32590 (comment) has discussed before) may be a simpler choice,

yeah, it's okay to go for this approach, one thing to note is that we'd prefer to see a plot example after this change, you could add the screenshot on top (e.g. in your PR description below your plan a/b).

Furthermore, I think it's nice to see that you already have a test which shows `datetime` input won't raise the error, and it will be nicer if you could add a test to show the output of the plot with a datetime input is correct! There are many examples in tests/plotting where you could use them as references to add a test! 
I'll go on to refactor the code and compliment related tests. This may take some time. Thanks for your advice @charlesdong1991 ! 
[WIP] todo: add new tests and pic
- The pic and copy-pastable example are updated in https://github.com/pandas-dev/pandas/pull/36287#issue-484893487
- Finally, https://github.com/pandas-dev/pandas/pull/36287#discussion_r491667937 is the best choice I think
- Related tests are modified to add a datetime column in original `df` data
@charlesdong1991 @jreback Do you have time to make a review, thanks!
@charlesdong1991 thanks for your suggestions, let me know if there's still any to improve :)
@jreback Can you take some time to make a review, thanks!
thanks @onshek very nice!