Skip to content

Commit e0710f3

Browse files
committed
Move to instance properties
1 parent 857364f commit e0710f3

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

pandas/core/indexes/base.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,6 @@ class Index(IndexOpsMixin, PandasObject):
244244
_engine_type = libindex.ObjectEngine
245245

246246
_accessors = set(['str'])
247-
# Whether items can be selected from NDFrame.<item>
248-
# Some indexes (DatetimeIndex, Int64Index) cannot contain
249-
# valid Python identifiers. Setting _can_hold_identifiers = False is an
250-
# optimization.
251-
# https://github.com/pandas-dev/pandas/issues/19764
252-
_can_hold_identifiers = True
253247

254248
str = CachedAccessor("str", StringMethods)
255249

@@ -2089,6 +2083,23 @@ def __getitem__(self, key):
20892083
else:
20902084
return result
20912085

2086+
@property
2087+
def _can_hold_identifiers(self):
2088+
"""
2089+
Whether the Index class *can* hold Python identifiers.
2090+
2091+
This is useful for short-circuting lookups in NDFrame.__getattr__.
2092+
Some index-classes can't hold identifiers (NumericIndex,
2093+
DatetimeIndex), so there's no reason to search the index when a user
2094+
does `Series.foo<TAB>`.
2095+
2096+
Note that we don't care about `foo` here. This just a property
2097+
of the index class itself, nothing to do with an instance.
2098+
2099+
https://github.com/pandas-dev/pandas/issues/19764
2100+
"""
2101+
return True
2102+
20922103
def append(self, other):
20932104
"""
20942105
Append a collection of Index options together

pandas/core/indexes/interval.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ class IntervalIndex(IntervalMixin, Index):
207207
_typ = 'intervalindex'
208208
_comparables = ['name']
209209
_attributes = ['name', 'closed']
210-
_can_hold_identifiers = False # can't contain Python identifiers
211210

212211
# we would like our indexing holder to defer to us
213212
_defer_to_indexing = True
@@ -1304,6 +1303,12 @@ def __getitem__(self, value):
13041303

13051304
return self._shallow_copy(left, right)
13061305

1306+
@property
1307+
def _can_hold_identifiers(self):
1308+
# perf: Intervals aren't valid Python identifiers.
1309+
# https://github.com/pandas-dev/pandas/issues/19764
1310+
return False
1311+
13071312
# __repr__ associated methods are based on MultiIndex
13081313

13091314
def _format_with_header(self, header, **kwargs):

pandas/core/indexes/numeric.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class NumericIndex(Index):
3131
3232
"""
3333
_is_numeric_dtype = True
34-
_can_hold_identifiers = False # Can't contain Python identifiers
3534

3635
def __new__(cls, data=None, dtype=None, copy=False, name=None,
3736
fastpath=False):
@@ -115,6 +114,12 @@ def is_all_dates(self):
115114
"""
116115
return False
117116

117+
@property
118+
def _can_hold_identifiers(self):
119+
# perf: Numeric elements are not valid identifiers.
120+
# https://github.com/pandas-dev/pandas/issues/19764
121+
return False
122+
118123

119124
_num_index_shared_docs['class_descr'] = """
120125
Immutable ndarray implementing an ordered, sliceable set. The basic object

pandas/core/indexes/period.py

-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ class PeriodIndex(DatelikeOps, DatetimeIndexOpsMixin, Int64Index):
204204
"""
205205
_typ = 'periodindex'
206206
_attributes = ['name', 'freq']
207-
_can_hold_identifiers = False # Can't contain Python identifiers
208207

209208
# define my properties & methods for delegation
210209
_other_ops = []

0 commit comments

Comments
 (0)