Skip to content

ENH: option to change the number in _dir_additions_for_owner #44335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
aee8dab
ENH: option to change the number in `__max_dir_additions`
KarstenNaert Nov 6, 2021
d9c311f
unittest for display.max_dir_items
KarstenNaert Nov 7, 2021
8ac71fd
add display.max_dir_items to docs
KarstenNaert Nov 7, 2021
fd85847
trim trailing whitespace
KarstenNaert Nov 7, 2021
ca36ecd
fix typo diplay -> display
KarstenNaert Nov 7, 2021
c66e7f1
Merge remote-tracking branch 'origin/master' into feature/max_dir_items
KarstenNaert Nov 7, 2021
26424f2
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 10, 2021
cab384d
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 11, 2021
b7c75fd
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 12, 2021
ec01193
Removed info about caching from options.rst
KarstenNaert Nov 13, 2021
1a97524
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 13, 2021
b86178e
Merge remote-tracking branch 'origin/master' into feature/max_dir_items
KarstenNaert Nov 15, 2021
5bd8336
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 15, 2021
9c0e8ff
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 16, 2021
090267e
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 18, 2021
31da398
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 20, 2021
634019c
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 22, 2021
2fb58b6
Merge remote-tracking branch 'upstream/master' into feature/max_dir_i…
KarstenNaert Nov 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/user_guide/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ display.html.use_mathjax True When True, Jupyter notebook
table contents using MathJax, rendering
mathematical expressions enclosed by the
dollar symbol.
display.max_dir_items 100 The number of columns from a dataframe that
are added to dir. These columns can then be
suggested by tab completion. 'None' value means
unlimited.
io.excel.xls.writer xlwt The default Excel writer engine for
'xls' files.

Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Other enhancements
- :meth:`.GroupBy.mean` now supports `Numba <http://numba.pydata.org/>`_ execution with the ``engine`` keyword (:issue:`43731`)
- :meth:`Timestamp.isoformat`, now handles the ``timespec`` argument from the base :class:``datetime`` class (:issue:`26131`)
- :meth:`NaT.to_numpy` ``dtype`` argument is now respected, so ``np.timedelta64`` can be returned (:issue:`44460`)
-
- New option ``display.max_dir_items`` customizes the number of columns added to :meth:`Dataframe.__dir__` and suggested for tab completion (:issue:`37996`)


.. ---------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ def use_numba_cb(key):
(default: True)
"""

pc_max_dir_items = """\
: int
The number of items that will be added to `dir(...)`. 'None' value means
unlimited. Because dir is cached, changing this option will not immediately
affect already existing dataframes until a column is deleted or added.

This is for instance used to suggest columns from a dataframe to tab
completion.
"""

pc_width_doc = """
: int
Width of the display in characters. In case python/IPython is running in
Expand Down Expand Up @@ -451,6 +461,9 @@ def _deprecate_negative_int_max_colwidth(key):
cf.register_option(
"html.use_mathjax", True, pc_html_use_mathjax_doc, validator=is_bool
)
cf.register_option(
"max_dir_items", 100, pc_max_dir_items, validator=is_nonnegative_int
)

tc_sim_interactive_doc = """
: boolean
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def _dir_additions_for_owner(self) -> set[str_t]:
"""
return {
c
for c in self.unique(level=0)[:100]
for c in self.unique(level=0)[: get_option("display.max_dir_items")]
if isinstance(c, str) and c.isidentifier()
}

Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import pytest

from pandas._config.config import option_context

import pandas.util._test_decorators as td
from pandas.util._test_decorators import (
async_mark,
Expand Down Expand Up @@ -87,6 +89,25 @@ def test_tab_completion(self):
assert key not in dir(df)
assert isinstance(df.__getitem__("A"), DataFrame)

def test_display_max_dir_items(self):
# display.max_dir_items increaes the number of columns that are in __dir__.
columns = ["a" + str(i) for i in range(420)]
values = [range(420), range(420)]
df = DataFrame(values, columns=columns)

# The default value for display.max_dir_items is 100
assert "a99" in dir(df)
assert "a100" not in dir(df)

with option_context("display.max_dir_items", 300):
df = DataFrame(values, columns=columns)
assert "a299" in dir(df)
assert "a300" not in dir(df)

with option_context("display.max_dir_items", None):
df = DataFrame(values, columns=columns)
assert "a419" in dir(df)

def test_not_hashable(self):
empty_frame = DataFrame()

Expand Down