ENH: Provide dict object for to_dict() #16122
State: closed

 - [x] closes #16122 
 - [x] tests added / passed
 - [x] passes ``git diff upstream/master --name-only -- '*.py' | flake8 --diff``
 - [x] whatsnew entry



Labels:
name: Enhancement
name: API Design

Comments:
Hey all,

I've implemented this for `pd.Series.to_dict()` and `pd.DataFrame.to_dict()`.  A few notes:

* I used the `into` keyword.
* For the `into` keyword, I check if the supplied class is a subclass of `collections.Mapping`.
* I had to do a few inelegant things to accommodate `collections.defaultdict`, since it takes it values  differently than the other mappings in `collections`.  We can remove this, but it felt odd to support the other `collections` mappings but not `defaultdict`.  For now, if the user supplies `defaultdict` to `into`, the function returns a `defaultdict` with `None` for a `default_factory`.  This is all in the docstring.
* This is marked for the v0.21.0 milestone, but I added it to the “whats new” for v0.20.0.

Let me know if any of the above should change.  
I haven't looked closely yet, but for the `defaultdict` stuff, do are you expecting an initialized `defaultdict`? i.e.

```python
df.to_dict(into=defaultdict)
```

or

```
df.to_dict(into=defaultdict(list))
```

I would only support the second.

> This is marked for the v0.21.0 milestone, but I added it to the “whats new” for v0.20.0.

There should be a 0.20.1 whatsnew. You can add it there since it's backwards-compatible.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=h1) Report
> Merging [#16220](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/02eafaf2b8e761779e31e185719c31b0778b5317?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `88.23%`.

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

```diff
@@            Coverage Diff             @@
##           master   #16220      +/-   ##
==========================================
- Coverage   90.86%   90.86%   -0.01%     
==========================================
  Files         162      162              
  Lines       50887    50895       +8     
==========================================
+ Hits        46240    46247       +7     
- Misses       4647     4648       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.65% <88.23%> (-0.01%)` | :arrow_down: |
| #single | `40.3% <17.64%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/series.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc2VyaWVzLnB5) | `94.92% <85.71%> (-0.08%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.54% <90%> (-0.05%)` | :arrow_down: |
| [pandas/core/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tbW9uLnB5) | `91.03% <0%> (+0.34%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16220?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/16220?src=pr&el=footer). Last update [02eafaf...9d5c8d2](https://codecov.io/gh/pandas-dev/pandas/pull/16220?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/16220?src=pr&el=h1) Report
> Merging [#16220](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/4bdbcb638340a0529c2ddc7d3a85d0209004763f?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #16220      +/-   ##
==========================================
- Coverage   90.38%   90.37%   -0.02%     
==========================================
  Files         161      161              
  Lines       50916    50930      +14     
==========================================
+ Hits        46021    46026       +5     
- Misses       4895     4904       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.14% <100%> (ø)` | :arrow_up: |
| #single | `40.21% <21.73%> (-0.12%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.69% <100%> (-0.1%)` | :arrow_down: |
| [pandas/core/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tbW9uLnB5) | `91.39% <100%> (+0.35%)` | :arrow_up: |
| [pandas/core/series.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc2VyaWVzLnB5) | `94.71% <100%> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/16220?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |

------

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

@jreback look good to you? Your requested changes were around `standardize_mapping`,  and I think they were all addressed.
lgtm. I think @jorisvandenbossche had a couple of small changes.
The appveyor build finished: https://ci.appveyor.com/project/pandas-dev/pandas/build/1.0.1881 Not sure why it hasn't reported that it's done.

Thanks @dwkenefick, nice work!