Skip to content

PERF: Faster Series.__getattribute__ #20834

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
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4375,8 +4375,7 @@ def __getattr__(self, name):
name in self._accessors):
return object.__getattribute__(self, name)
else:
if (self._info_axis._can_hold_identifiers and
name in self._info_axis):
if self._info_axis._can_hold_identifiers_and_holds_name(name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much nicer! thanks!

return self[name]
return object.__getattribute__(self, name)

Expand Down
20 changes: 8 additions & 12 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2083,22 +2083,18 @@ def __getitem__(self, key):
else:
return result

@property
def _can_hold_identifiers(self):
def _can_hold_identifiers_and_holds_name(self, name):
"""
Whether the Index class *can* hold Python identifiers.

This is useful for short-circuting lookups in NDFrame.__getattr__.
Some index-classes can't hold identifiers (NumericIndex,
DatetimeIndex), so there's no reason to search the index when a user
does `Series.foo<TAB>`.

Note that we don't care about `foo` here. This just a property
of the index class itself, nothing to do with an instance.
Faster check for ``name in self`` when we know `name` is a Python
identifier (e.g. in NDFrame.__getattr__, which hits this to support
. key lookup). For indexes that can't hold identifiers (everything
but object & categorical) we just return False.

https://github.com/pandas-dev/pandas/issues/19764
"""
return True
if self.is_object() or self.is_categorical():
return name in self
return False

def append(self, other):
"""
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,12 +1303,6 @@ def __getitem__(self, value):

return self._shallow_copy(left, right)

@property
def _can_hold_identifiers(self):
# perf: Intervals aren't valid Python identifiers.
# https://github.com/pandas-dev/pandas/issues/19764
return False

# __repr__ associated methods are based on MultiIndex

def _format_with_header(self, header, **kwargs):
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ def is_all_dates(self):
"""
return False

@property
def _can_hold_identifiers(self):
# perf: Numeric elements are not valid identifiers.
# https://github.com/pandas-dev/pandas/issues/19764
return False


_num_index_shared_docs['class_descr'] = """
Immutable ndarray implementing an ordered, sliceable set. The basic object
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class DatetimeLike(Base):

def test_can_hold_identifiers(self):
idx = self.create_index()
assert idx._can_hold_identifiers is False
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is False

def test_shift_identity(self):

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def generate_index_types(self, skip_index_keys=[]):

def test_can_hold_identifiers(self):
idx = self.create_index()
assert idx._can_hold_identifiers is True
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is True

def test_new_axis(self):
new_index = self.dateIndex[None, :]
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def create_index(self, categories=None, ordered=False):
list('aabbca'), categories=categories, ordered=ordered)

def test_can_hold_identifiers(self):
ci = self.create_index(categories=list('abcd'))
assert ci._can_hold_identifiers is True
idx = self.create_index(categories=list('abcd'))
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is True

def test_construction(self):

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def create_index(self):

def test_can_hold_identifiers(self):
idx = self.create_index()
assert idx._can_hold_identifiers is True
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is True

def test_boolean_context_compat2(self):

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class Numeric(Base):

def test_can_hold_identifiers(self):
idx = self.create_index()
assert idx._can_hold_identifiers is False
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is False

def test_numeric_compat(self):
pass # override Base method
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def check_binop(self, ops, scalars, idxs):

def test_can_hold_identifiers(self):
idx = self.create_index()
assert idx._can_hold_identifiers is False
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is False

def test_binops(self):
ops = [operator.add, operator.sub, operator.mul, operator.floordiv,
Expand Down