From aee8dab4914ab79f6c5c2d1168ad84edc63a2e45 Mon Sep 17 00:00:00 2001 From: Karsten Naert Date: Sat, 6 Nov 2021 17:08:42 +0100 Subject: [PATCH 1/6] ENH: option to change the number in `__max_dir_additions` --- pandas/core/config_init.py | 13 +++++++++++++ pandas/core/indexes/base.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 0081f8cd074b6..f3957d4f9ed7f 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -236,6 +236,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 @@ -449,6 +459,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 ba7dde7d2a4d8..4ab85ad164927 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() } From d9c311f5a8fa1959f7c89bc6bd27c22004a285c4 Mon Sep 17 00:00:00 2001 From: Karsten Naert Date: Sun, 7 Nov 2021 11:23:17 +0100 Subject: [PATCH 2/6] unittest for display.max_dir_items --- pandas/tests/frame/test_api.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 49649c1487f13..71d4225aad44b 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 diplay.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() From 8ac71fd8080e8c43294469ab53c63008d979ee15 Mon Sep 17 00:00:00 2001 From: Karsten Naert Date: Sun, 7 Nov 2021 12:38:36 +0100 Subject: [PATCH 3/6] add display.max_dir_items to docs --- doc/source/user_guide/options.rst | 8 ++++++++ doc/source/whatsnew/v1.4.0.rst | 1 + 2 files changed, 9 insertions(+) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index a65bb774b9df8..db3ac0a40a02f 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -430,6 +430,14 @@ 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. + Due to caching changing the setting + will only affect new dataframes or dataframes + where the cache was invalidated, for instance + because a column was removed or added. 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 66b26e56b2258..666d8705d431f 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -184,6 +184,7 @@ Other enhancements - :meth:`DataFrame.dropna` now accepts a single label as ``subset`` along with array-like (:issue:`41021`) - :meth:`read_excel` now accepts a ``decimal`` argument that allow the user to specify the decimal point when parsing string columns to numeric (:issue:`14403`) - :meth:`.GroupBy.mean` now supports `Numba `_ execution with the ``engine`` keyword (:issue:`43731`) +- New option ``display.max_dir_items`` customizes the number of columns added to :meth:`Dataframe.__dir__` and suggested for tab completion (:issue:`37996`) .. --------------------------------------------------------------------------- From fd85847dd1d93223b339ba7047a681f9525374ac Mon Sep 17 00:00:00 2001 From: Karsten Naert Date: Sun, 7 Nov 2021 12:46:39 +0100 Subject: [PATCH 4/6] trim trailing whitespace --- doc/source/user_guide/options.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index db3ac0a40a02f..4deac25634419 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -431,7 +431,7 @@ display.html.use_mathjax True When True, Jupyter notebook 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 + are added to dir. These columns can then be suggested by tab completion. 'None' value means unlimited. Due to caching changing the setting From ca36ecd779f884056c50eabe3c6d99e2bc78b730 Mon Sep 17 00:00:00 2001 From: Karsten Naert Date: Sun, 7 Nov 2021 12:56:14 +0100 Subject: [PATCH 5/6] fix typo diplay -> display --- pandas/tests/frame/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 71d4225aad44b..5bf2cd7ae0468 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -95,7 +95,7 @@ def test_display_max_dir_items(self): values = [range(420), range(420)] df = DataFrame(values, columns=columns) - # The default value for diplay.max_dir_items is 100 + # The default value for display.max_dir_items is 100 assert "a99" in dir(df) assert "a100" not in dir(df) From ec011930cb77d39d1bf87aa1f31f5d4b45a72a9f Mon Sep 17 00:00:00 2001 From: Karsten Naert Date: Sat, 13 Nov 2021 20:06:37 +0100 Subject: [PATCH 6/6] Removed info about caching from options.rst --- doc/source/user_guide/options.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 4deac25634419..93448dae578c9 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -434,10 +434,6 @@ display.max_dir_items 100 The number of columns from are added to dir. These columns can then be suggested by tab completion. 'None' value means unlimited. - Due to caching changing the setting - will only affect new dataframes or dataframes - where the cache was invalidated, for instance - because a column was removed or added. io.excel.xls.writer xlwt The default Excel writer engine for 'xls' files.