Skip to content

Commit

Permalink
BUG: Allow indexing with namedtuple, close #1026
Browse files Browse the repository at this point in the history
  • Loading branch information
kisielk authored and wesm committed Apr 14, 2012
1 parent cff50cd commit 0b5a007
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __iter__(self):
raise NotImplementedError('ix is not iterable')

def __getitem__(self, key):
if isinstance(key, tuple):
if type(key) is tuple:
try:
return self.obj.get_value(*key)
except Exception:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5114,6 +5114,15 @@ def test_stale_cached_series_bug_473(self):
exp = Y['g'].sum()
self.assert_(isnull(Y['g']['c']))

def test_index_namedtuple(self):
from collections import namedtuple
IndexType = namedtuple("IndexType", ["a", "b"])
idx1 = IndexType("foo", "bar")
idx2 = IndexType("baz", "bof")
index = Index([idx1, idx2], name="composite_index")
df = DataFrame([(1, 2), (3, 4)], index=index, columns=["A", "B"])
self.assertEqual(df.ix[IndexType("foo", "bar")], (1, 2))

if __name__ == '__main__':
# unittest.main()
import nose
Expand Down

0 comments on commit 0b5a007

Please sign in to comment.