diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index a65bb774b9df8..93448dae578c9 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -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. diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index ad6bf64dcdfa8..64bdbca9c1f27 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -213,7 +213,7 @@ Other enhancements - :meth:`.GroupBy.mean` now supports `Numba `_ 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`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 31c2ec8f0cbf9..bf2d770ee1e7f 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -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 @@ -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 diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 220b43f323a5f..5f7bc718215be 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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() } diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 2e276f4f27a67..3adc4ebceaad5 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -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, @@ -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()