Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 9 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5175,10 +5175,16 @@ def _dir_additions(self):
add the string-like attributes from the info_axis.
If info_axis is a MultiIndex, it's first level values are used.
"""
unique_entries = self._info_axis.unique(level=0)
if len(unique_entries) > 1000:
unique_entries = unique_entries[:1000]
warnings.warn(
"Completion only considers the first 1000 entries "
"for performance reasons",
UserWarning,
)
additions = {
c
for c in self._info_axis.unique(level=0)[:100]
if isinstance(c, str) and c.isidentifier()
c for c in unique_entries if isinstance(c, str) and c.isidentifier()
}
return super()._dir_additions().union(additions)

Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,22 @@ def get_dir(s):
tm.makeIntIndex(10),
tm.makeFloatIndex(10),
Index([True, False]),
Index([f"a{i}" for i in range(101)]),
Index([f"a{i}" for i in range(1001)]),
pd.MultiIndex.from_tuples(zip("ABCD", "EFGH")),
pd.MultiIndex.from_tuples(zip([0, 1, 2, 3], "EFGH")),
],
)
def test_index_tab_completion(self, index):
# dir contains string-like values of the Index.
s = pd.Series(index=index, dtype=object)
dir_s = dir(s)
if len(s) < 1000:
dir_s = dir(s)
else:
with tm.assert_produces_warning(UserWarning):
dir_s = dir(s)

for i, x in enumerate(s.index.unique(level=0)):
if i < 100:
if i < 1000:
assert not isinstance(x, str) or not x.isidentifier() or x in dir_s
else:
assert x not in dir_s
Expand Down