ENH: _dir_additions returns also the first level of a MultiIndex
State: closed

 - [x] tests added / passed
 - [x] passes ``git diff upstream/master --name-only -- '*.py' | flake8 --diff``
 - [x] whatsnew entry

Assuming a DataFrame with MultiIndex columns like this:
```python
>> df
    foo   bar      
          foo   bar
0  0.32  0.15  0.45
1  0.62  0.73  0.36
2  0.21  0.68  0.10
3  0.05  0.36  0.90
```

one can access to `df['foo']` and `df['bar']` with the shortcuts `df.foo` and `df.bar`,
but one don't benefit from autocompletion because 'foo' and 'bar' are not listed in `dir(df)`. 

```python
>> 'bar' in dir(df)
False
```

This PR extends `df._dir_additions` so that the first level of a MultiIndex is listed in `dir(df)`.

```python
>> 'bar' in dir(df)
True
```


Labels:
name: Output-Formatting
description: __repr__ of pandas objects, to_string
name: MultiIndex

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16326?src=pr&el=h1) Report
> Merging [#16326](https://codecov.io/gh/pandas-dev/pandas/pull/16326?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/0607e03f71b52e131947d73880380796f51d72fb?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `n/a`.

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

```diff
@@            Coverage Diff             @@
##           master   #16326      +/-   ##
==========================================
+ Coverage   90.37%   90.37%   +<.01%     
==========================================
  Files         161      161              
  Lines       50863    50863              
==========================================
+ Hits        45966    45969       +3     
+ Misses       4897     4894       -3
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.16% <ø> (+0.02%)` | :arrow_up: |
| #single | `40.33% <ø> (-0.11%)` | :arrow_down: |

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

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16326?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/16326?src=pr&el=footer). Last update [0607e03...456a932](https://codecov.io/gh/pandas-dev/pandas/pull/16326?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/16326?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@e909ea0`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff            @@
##             master   #16326   +/-   ##
=========================================
  Coverage          ?   91.56%           
=========================================
  Files             ?      153           
  Lines             ?    51272           
  Branches          ?        0           
=========================================
  Hits              ?    46947           
  Misses            ?     4325           
  Partials          ?        0
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.42% <100%> (?)` | |
| #single | `40.68% <0%> (?)` | |

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

------

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

I am +0 on this. This add some needless complexity, though from a user POV might be nice. Would need some additional tests to consider.
@jreback 
> Would need some additional tests to consider.

Sure. I plan to put tests if the principle is accepted. Any idea is welcome.
Btw, I've not seen whether there are tests about `dir` behavior.
> Btw, I've not seen whether there are tests about dir behavior.

``bash-3.2$ grep -R 'dir(' pandas/tests/``

Added `test_tab_completion` for DataFrames, inpired from Series.
@jorisvandenbossche thoughts here
this is ok in principle. can you rebase / update
can you rebase
Sorry, I've been long to rebase.

Do I need to add a whatsneww entry ? In which version file ?
Changes are done, and rebased. Btw, I've added a test on Series auto-completion.
one additional thing I think we need. If you have a very large index, ``_dir_additions`` actually takes quite a bit of time (this is not exclusive of this change).

So what I would do is if the index is say < 100, use the currently ``_dir_addition``, otherwise return an empty list! (its essentially too big to use tab completion for anyhow). can you make this change and add an asv for this (could be a separate PR as well)
xref https://github.com/pandas-dev/pandas/issues/18587
you can actually use ``.unique(level=0)`` to make this efficient
I think that it's ready to go ; please tell me if I shall squash the commits.
Rebased and ready
thanks @BibMartin nice patch!
Thanks to you @jreback : it's been a great occasion to learn for me.