BUG: support dtypes in column_dtypes for to_records()
State: closed

Despite the name, the `column_dtypes` param in `DataFrame.to_records()` doesn't accept `np.dtype` objects:
```
In [3]: df = pd.DataFrame([[0, 1], [2, 3]])
In [4]: df.to_records(column_dtypes=df.dtypes)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-e3541f39084e> in <module>()
----> 1 df.to_records(column_dtypes=df.dtypes)

~/projects/pandas/pandas/core/frame.py in to_records(self, index, convert_datetime64, column_dtypes, index_dtypes)
   1724                 msg = ("Invalid dtype {dtype} specified for "
   1725                        "{element} {name}").format(dtype=dtype_mapping,
-> 1726                                                   element=element, name=name)
   1727                 raise ValueError(msg)
   1728 

ValueError: Invalid dtype int64 specified for column 0
```
The above also fails when `df.dtypes` is cast into a `dict` for overriding.

This PR simply adds `np.dtype` to the accepted types and adopts a subset of the tests that currently cover the numpy scalar types (`np.int64` and similar, which are not dtypes).

- [ ] closes #xxxx
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Enhancement
name: Output-Formatting
description: __repr__ of pandas objects, to_string
name: Dtype Conversions
description: Unexpected or buggy dtype conversions

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24895?src=pr&el=h1) Report
> Merging [#24895](https://codecov.io/gh/pandas-dev/pandas/pull/24895?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/2fa0835737a0e3e111e893d30ed2f25b7249fd4b?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/24895/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24895?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #24895      +/-   ##
==========================================
+ Coverage   92.38%   92.39%   +<.01%     
==========================================
  Files         166      166              
  Lines       52413    52413              
==========================================
+ Hits        48424    48425       +1     
+ Misses       3989     3988       -1
```

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

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24895?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/24895/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `96.92% <100%> (ø)` | :arrow_up: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/24895/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `88.13% <0%> (+0.09%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24895?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/24895?src=pr&el=footer). Last update [2fa0835...a86d008](https://codecov.io/gh/pandas-dev/pandas/pull/24895?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/24895?src=pr&el=h1) Report
> Merging [#24895](https://codecov.io/gh/pandas-dev/pandas/pull/24895?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/b9f2e2bd526ad69fe3d737d395742193e29fd0e5?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

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

```diff
@@           Coverage Diff           @@
##           master   #24895   +/-   ##
=======================================
  Coverage   92.38%   92.38%           
=======================================
  Files         166      166           
  Lines       52401    52401           
=======================================
  Hits        48409    48409           
  Misses       3992     3992
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.8% <100%> (ø)` | :arrow_up: |
| #single | `42.88% <0%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24895?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/24895/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `96.92% <100%> (ø)` | :arrow_up: |

------

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

cc @jreback 

@qwhelan : Thanks for the PR!  I do see however that our docstring explicitly does not mention `np.dtype` as an accepted data type for this parameter (see [here](https://github.com/pandas-dev/pandas/blob/a86d008be325fea200a9c5f328e77f652d69045e/pandas/core/frame.py#L1566)).  This is thus not a bug, but an enhancement that you are proposing.
@gfyoung My initial thought was that this was an enhancement until I read the discussion on #22229, where the intent seemed to be to support `np.dtype` as well: https://github.com/pandas-dev/pandas/pull/22229/files#r244338742
> type should be `numpy.dtype` right? Oh, but we probably also accept things like `int`, which is also a type.
> Yep, that’s why I use type and not numpy.dtype.

Entirely possible I'm reading it wrong, but it looks like there was an assumption that `type` would handle `np.dtype` and `int`/etc (it only covers the latter). Additional evidence here is that the parameter name includes `dtype`, the docstring references `data types`, and the function itself uses `np.dtype` as a valid input:
```python
if dtype_mapping is None:
    formats.append(v.dtype)
elif isinstance(dtype_mapping, (type, compat.string_types)):
    formats.append(dtype_mapping)
...
```
xref: https://github.com/pandas-dev/pandas/pull/22229#discussion_r244338742

Fair enough.  I'm not opposed to adding this *per se*.  It was more of a question of semantics regarding bug and enhancement.
@gfyoung Totally understand on the semantics; just trying to clarify why I see this as deviating from expected behavior.
Hello @qwhelan! Thanks for updating the PR.





Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on January 30, 2019 at 17:42 Hours UTC
lgtm. can you add a whatsnew note (0.25.0), bug fixes reshaping. @gfyoung if you can have a look.
@jreback Done
@jreback : This looks good to me.  Not sure if you wanted to have a look once more?
thanks @qwhelan 
Thanks! Small perf improvement built on this coming soon