API: more consistent error message for MultiIndex.from_arrays
State: closed

improve error messages: xref https://github.com/pandas-dev/pandas/issues/23922#issuecomment-441652717

a small change to start. @jorisvandenbossche: do we have/need a master tracker/discussion issue for improving error messages?


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

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

```diff
@@            Coverage Diff             @@
##           master   #25189      +/-   ##
==========================================
+ Coverage   92.34%   92.34%   +<.01%     
==========================================
  Files         166      166              
  Lines       52310    52316       +6     
==========================================
+ Hits        48304    48310       +6     
  Misses       4006     4006
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.78% <100%> (ø)` | :arrow_up: |
| #single | `42.84% <50%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25189?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/multi.py](https://codecov.io/gh/pandas-dev/pandas/pull/25189/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9tdWx0aS5weQ==) | `95.62% <100%> (+0.02%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25189?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/25189?src=pr&el=footer). Last update [09633b8...0974cee](https://codecov.io/gh/pandas-dev/pandas/pull/25189?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/25189?src=pr&el=h1) Report
> Merging [#25189](https://codecov.io/gh/pandas-dev/pandas/pull/25189?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/13a505d5541a8fab878c2175f60328f5816b4d25?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/25189/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/25189?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #25189      +/-   ##
==========================================
+ Coverage   91.73%   91.73%   +<.01%     
==========================================
  Files         173      173              
  Lines       52841    52845       +4     
==========================================
+ Hits        48476    48480       +4     
  Misses       4365     4365
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.3% <100%> (ø)` | :arrow_up: |
| #single | `41.72% <60%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25189?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/multi.py](https://codecov.io/gh/pandas-dev/pandas/pull/25189/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9tdWx0aS5weQ==) | `95.62% <100%> (+0.01%)` | :arrow_up: |

------

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

we could replace `Input must be a list / sequence of array-likes` with `Input must be a list / sequence of equal length array-likes` and consolidate with the `all arrays must be same length` message.

..but that could be considered reducing the helpfulness of the messaging.
I don’t think you necessarily need to consolidate. Why not have different error messages?

Sent from my iPhone

> On Feb 6, 2019, at 7:56 AM, Simon Hawkins <notifications@github.com> wrote:
> 
> we could replace Input must be a list / sequence of array-likes with Input must be a list / sequence of equal length array-likes and consolidate with the all arrays must be same length message.
> 
> ..but that could be considered reducing the helpfulness of the messaging.
> 
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub, or mute the thread.

There's helpfulness and handholding.

the api reference states what the input should be. the error message should probably just regurgitate this.

```
arrays : list / sequence of array-likes
```

a single error message for each incorrect input would probably be more consistent than many variations.
regarding the all arrays must be same length message, xref #13599, which is effectively an invalid input the same as `Input must be a list / sequence of array-likes`, this perhaps provides an opportunity to improve the docstring and perhaps merge `test_from_arrays_invalid_input` and `test_from_arrays_different_lengths`.

however, i've slightly sidetracked from the original change which changes

```python-traceback
>>> from pandas import MultiIndex
>>> MultiIndex.from_arrays([1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\indexes\multi.py", line 340, in from_arrays
    codes, levels = _factorize_from_iterables(arrays)
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\arrays\categorical.py", line 2710, in _factorize_from_iterables
    return map(list, lzip(*[_factorize_from_iterable(it) for it in iterables]))
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\arrays\categorical.py", line 2710, in <listcomp>
    return map(list, lzip(*[_factorize_from_iterable(it) for it in iterables]))
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\arrays\categorical.py", line 2671, in _factorize_from_iterable
    raise TypeError("Input must be list-like")
TypeError: Input must be list-like
>>>
```
to be more meaningful. `[1]` is list-like!


and changes..

```python-traceback
>>> MultiIndex.from_arrays([[1],1], sortorder=1, names=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\indexes\multi.py", line 335, in from_arrays
    if len(arrays[i]) != len(arrays[i - 1]):
TypeError: object of type 'int' has no len()
>>>
>>>
```
to be more meaningful and much more helpful. which int have i got wrong?

both these cases will raise `TypeError: Input must be a list / sequence of array-likes` with this change.
I recall this exact change from another issue, can you see if you can find it (search for MultiIndex issues). 
> I recall this exact change from another issue, can you see if you can find it (search for MultiIndex issues).

#18434 and subsequent PR #18440 is along the same theme and prevents a `TypeError: object of type 'zip' has no len()`.

Here, we are catching the len issue on elements of the list / sequence which should also be list-like.
lgtm. @jorisvandenbossche were you ok on this?
@simonjayhawkins can you merge master?

@jorisvandenbossche unless you have objections planning to merge after above
Yes, sure!
lgtm. can merge on green after merging master.
> can merge on green after merging master

@jreback: merged master.
Thanks @simonjayhawkins !