Skip to content

Commit 068b599

Browse files
authored
ENH: option to change the number in _dir_additions_for_owner (#44335)
1 parent 5ee8cf1 commit 068b599

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

doc/source/user_guide/options.rst

+4
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ display.html.use_mathjax True When True, Jupyter notebook
430430
table contents using MathJax, rendering
431431
mathematical expressions enclosed by the
432432
dollar symbol.
433+
display.max_dir_items 100 The number of columns from a dataframe that
434+
are added to dir. These columns can then be
435+
suggested by tab completion. 'None' value means
436+
unlimited.
433437
io.excel.xls.writer xlwt The default Excel writer engine for
434438
'xls' files.
435439

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Other enhancements
213213
- :meth:`.GroupBy.mean` now supports `Numba <http://numba.pydata.org/>`_ execution with the ``engine`` keyword (:issue:`43731`)
214214
- :meth:`Timestamp.isoformat`, now handles the ``timespec`` argument from the base :class:``datetime`` class (:issue:`26131`)
215215
- :meth:`NaT.to_numpy` ``dtype`` argument is now respected, so ``np.timedelta64`` can be returned (:issue:`44460`)
216-
-
216+
- New option ``display.max_dir_items`` customizes the number of columns added to :meth:`Dataframe.__dir__` and suggested for tab completion (:issue:`37996`)
217217

218218

219219
.. ---------------------------------------------------------------------------

pandas/core/config_init.py

+13
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ def use_numba_cb(key):
238238
(default: True)
239239
"""
240240

241+
pc_max_dir_items = """\
242+
: int
243+
The number of items that will be added to `dir(...)`. 'None' value means
244+
unlimited. Because dir is cached, changing this option will not immediately
245+
affect already existing dataframes until a column is deleted or added.
246+
247+
This is for instance used to suggest columns from a dataframe to tab
248+
completion.
249+
"""
250+
241251
pc_width_doc = """
242252
: int
243253
Width of the display in characters. In case python/IPython is running in
@@ -451,6 +461,9 @@ def _deprecate_negative_int_max_colwidth(key):
451461
cf.register_option(
452462
"html.use_mathjax", True, pc_html_use_mathjax_doc, validator=is_bool
453463
)
464+
cf.register_option(
465+
"max_dir_items", 100, pc_max_dir_items, validator=is_nonnegative_int
466+
)
454467

455468
tc_sim_interactive_doc = """
456469
: boolean

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ def _dir_additions_for_owner(self) -> set[str_t]:
846846
"""
847847
return {
848848
c
849-
for c in self.unique(level=0)[:100]
849+
for c in self.unique(level=0)[: get_option("display.max_dir_items")]
850850
if isinstance(c, str) and c.isidentifier()
851851
}
852852

pandas/tests/frame/test_api.py

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
from pandas._config.config import option_context
9+
810
import pandas.util._test_decorators as td
911
from pandas.util._test_decorators import (
1012
async_mark,
@@ -87,6 +89,25 @@ def test_tab_completion(self):
8789
assert key not in dir(df)
8890
assert isinstance(df.__getitem__("A"), DataFrame)
8991

92+
def test_display_max_dir_items(self):
93+
# display.max_dir_items increaes the number of columns that are in __dir__.
94+
columns = ["a" + str(i) for i in range(420)]
95+
values = [range(420), range(420)]
96+
df = DataFrame(values, columns=columns)
97+
98+
# The default value for display.max_dir_items is 100
99+
assert "a99" in dir(df)
100+
assert "a100" not in dir(df)
101+
102+
with option_context("display.max_dir_items", 300):
103+
df = DataFrame(values, columns=columns)
104+
assert "a299" in dir(df)
105+
assert "a300" not in dir(df)
106+
107+
with option_context("display.max_dir_items", None):
108+
df = DataFrame(values, columns=columns)
109+
assert "a419" in dir(df)
110+
90111
def test_not_hashable(self):
91112
empty_frame = DataFrame()
92113

0 commit comments

Comments
 (0)