Skip to content

Commit 0b5a007

Browse files
kisielkwesm
authored andcommitted
BUG: Allow indexing with namedtuple, close #1026
1 parent cff50cd commit 0b5a007

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __iter__(self):
2424
raise NotImplementedError('ix is not iterable')
2525

2626
def __getitem__(self, key):
27-
if isinstance(key, tuple):
27+
if type(key) is tuple:
2828
try:
2929
return self.obj.get_value(*key)
3030
except Exception:

pandas/tests/test_frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -5114,6 +5114,15 @@ def test_stale_cached_series_bug_473(self):
51145114
exp = Y['g'].sum()
51155115
self.assert_(isnull(Y['g']['c']))
51165116

5117+
def test_index_namedtuple(self):
5118+
from collections import namedtuple
5119+
IndexType = namedtuple("IndexType", ["a", "b"])
5120+
idx1 = IndexType("foo", "bar")
5121+
idx2 = IndexType("baz", "bof")
5122+
index = Index([idx1, idx2], name="composite_index")
5123+
df = DataFrame([(1, 2), (3, 4)], index=index, columns=["A", "B"])
5124+
self.assertEqual(df.ix[IndexType("foo", "bar")], (1, 2))
5125+
51175126
if __name__ == '__main__':
51185127
# unittest.main()
51195128
import nose

0 commit comments

Comments
 (0)