One of the useful features of IPython is its tab completion, among other things eliminating the need to memorize pandas.DataFrame column names.
E.g., suppose we have
df = pd.DataFrame({'bar': [1, 2], 'baz': [3, 4], 'bap': [5, 6]})
giving df as
bap bar baz
0 5 1 3
1 6 2 4
Then we can type
df.<Tab>
and it will show bap, bar, and baz (among others) as members, as well as attempting to complete them.
Unfortunately, this useful functionality partially disappears with hierarchical dataframes. E.g., if we change things to
df = pd.DataFrame({
('foo', 'bar'): [1, 2],
('foo', 'baz'): [3, 4],
('bap', ''): [5, 6]})
giving df as
bap foo
bar baz
0 5 1 3
1 6 2 4
then df.<Tab> will not autocomplete bap or foo.
What is the proper way to get this working? I have written a hack which does this (answer below), but am displeased with its reliance on Python monkey-patching. Other answers would be welcome.