EHN: Improve from_items error message (#17312)
State: closed

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


Labels:
name: Error Reporting
description: Incorrect or improved errors from pandas

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

```diff
@@            Coverage Diff             @@
##           master   #17881      +/-   ##
==========================================
+ Coverage   91.23%   91.24%   +<.01%     
==========================================
  Files         163      163              
  Lines       50102    50106       +4     
==========================================
+ Hits        45712    45719       +7     
+ Misses       4390     4387       -3
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.05% <100%> (+0.02%)` | :arrow_up: |
| #single | `40.31% <0%> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.75% <100%> (-0.1%)` | :arrow_down: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/plotting/\_converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb252ZXJ0ZXIucHk=) | `65.2% <0%> (+1.81%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17881?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/17881?src=pr&el=footer). Last update [aed9b92...a440e50](https://codecov.io/gh/pandas-dev/pandas/pull/17881?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/17881?src=pr&el=h1) Report
> Merging [#17881](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/38f41e64f4b8a0479f8835022af5e7343ccf8498?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #17881      +/-   ##
==========================================
+ Coverage    91.3%   91.32%   +0.02%     
==========================================
  Files         163      163              
  Lines       49781    49789       +8     
==========================================
+ Hits        45451    45471      +20     
+ Misses       4330     4318      -12
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.12% <100%> (+0.02%)` | :arrow_up: |
| #single | `40.71% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.81% <100%> (ø)` | :arrow_up: |
| [pandas/plotting/\_converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/17881?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb252ZXJ0ZXIucHk=) | `65.25% <0%> (+1.81%)` | :arrow_up: |

------

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

can you add a note on 0.22
The current behaviour of ``from_items`` on master when passed a (key, value) pair with a scalar value is:
```
In [1]: pd.DataFrame.from_items([('a', 1), ('b', 2)])
Out[1]: ValueError: If using all scalar values, you must pass an index 

In [2]: pd.DataFrame.from_items([('a', 1), ('b', 2)], columns=['col1'], orient='index')
Out[2]: TypeError: Argument 'objects' has incorrect type (expected numpy.ndarray, got int)
```
These error messages are not very helpful (``from_items`` doesn't have an ``index`` parameter, for example). So I have tried to provide more informative error messages:
```
In [3]: pd.DataFrame.from_items([('a', 1), ('b', 2)])
Out[3]: TypeError: The value in each (key, value) pair must be an array, Series, or dict

In [4]: pd.DataFrame.from_items([('a', 1), ('b', 2)], columns=['col1'], orient='index')
Out[4]: TypeError: The value in each (key, value) pair must be an array, Series, or dict
```
On the current master ``pd.DataFrame(dict(..))`` and ``pd.DataFrame.from_dict(dict(..))`` raise the same error message as ``from_items`` when passed scalar values: 
```
In [5]: pd.DataFrame({'A': 1, 'B': 2})
Out[5]: ValueError: If using all scalar values, you must pass an index 

In [6]: pd.DataFrame.from_dict({'A': 1, 'B': 2})
Out[6]: ValueError: If using all scalar values, you must pass an index 
```
However, trying to change these error messages in a similar way to ``from_items`` caused problems and seems to affect other functions such as ``df.agg({})``:
```
In [7]: pd.DataFrame({'A': 1, 'B': 2})
Out[7]: TypeError: The value in each key:value pair must be an array, Series, or dict

In [8]: pd.DataFrame.from_dict({'A': 1, 'B': 2})
Out[8]: TypeError: The value in each key:value pair must be an array, Series, or dict

In [9]:df = pd.DataFrame({'A': np.random.randn(10), 'B': np.random.randn(10)})
In [10]:df.agg({'A': 'mean'})
Out[10]:
RecursionError: maximum recursion depth exceeded
```
So in the updated pull request I have only changed the error message for ``from_items``. I haven't changed the error message for ``pd.DataFrame(dict(..))`` or ``from_dict`` but have modified the ``pd.DataFrame(dict(..))`` tests to check for the current error message. Is this solution acceptable or, if not, does anyone have any suggestions on how to proceed? Or maybe ``pd.DataFrame(dict(..))`` or ``from_dict`` could be discussed in a separate issue if needed?
can you rebase / update
one more rebase  and should be good
lgtm. ping on green.
@jreback thanks. It's green now.
thanks!
as an aside I think its ok to deprecate ``.from_items``, e.g. https://github.com/pandas-dev/pandas/issues/18262 as its trivially replace by ``dict(...)``