Skip to content

Commit eb39b44

Browse files
WillAydjreback
authored andcommitted
BUG: MultiIndex not raising AttributeError with a million records (#18165) (#18229)
1 parent bd62014 commit eb39b44

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Conversion
7373
Indexing
7474
^^^^^^^^
7575

76-
-
76+
- Bug where a ``MultiIndex`` with more than a million records was not raising ``AttributeError`` when trying to access a missing attribute (:issue:`18165`)
7777
-
7878
-
7979

pandas/core/indexes/multi.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,17 @@ def _shallow_copy_with_infer(self, values=None, **kwargs):
446446
**kwargs)
447447
return self._shallow_copy(values, **kwargs)
448448

449+
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
450+
def __contains__(self, key):
451+
hash(key)
452+
try:
453+
self.get_loc(key)
454+
return True
455+
except (LookupError, TypeError):
456+
return False
457+
458+
contains = __contains__
459+
449460
@Appender(_index_shared_docs['_shallow_copy'])
450461
def _shallow_copy(self, values=None, **kwargs):
451462
if values is not None:
@@ -1370,17 +1381,6 @@ def nlevels(self):
13701381
def levshape(self):
13711382
return tuple(len(x) for x in self.levels)
13721383

1373-
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
1374-
def __contains__(self, key):
1375-
hash(key)
1376-
try:
1377-
self.get_loc(key)
1378-
return True
1379-
except LookupError:
1380-
return False
1381-
1382-
contains = __contains__
1383-
13841384
def __reduce__(self):
13851385
"""Necessary for making this object picklable"""
13861386
d = dict(levels=[lev for lev in self.levels],

pandas/tests/indexes/test_multi.py

+10
Original file line numberDiff line numberDiff line change
@@ -2981,3 +2981,13 @@ def test_nan_stays_float(self):
29812981
assert pd.isna(df0.index.get_level_values(1)).all()
29822982
# the following failed in 0.14.1
29832983
assert pd.isna(dfm.index.get_level_values(1)[:-1]).all()
2984+
2985+
def test_million_record_attribute_error(self):
2986+
# GH 18165
2987+
r = list(range(1000000))
2988+
df = pd.DataFrame({'a': r, 'b': r},
2989+
index=pd.MultiIndex.from_tuples([(x, x) for x in r]))
2990+
2991+
with tm.assert_raises_regex(AttributeError,
2992+
"'Series' object has no attribute 'foo'"):
2993+
df['a'].foo()

0 commit comments

Comments
 (0)