Skip to content

BUG: allow get default value upon IndexError, GH #7725 #7728

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 1 commit into from
Sep 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ There are no experimental changes in 0.15.0
Bug Fixes
~~~~~~~~~

- Bug in ``get`` where an ``IndexError`` would not cause the default value to be returned (:issue:`7725`)



Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def get(self, key, default=None):
"""
try:
return self[key]
except (KeyError, ValueError):
except (KeyError, ValueError, IndexError):
return default

def __getitem__(self, item):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ def test_get_numeric_data(self):

# _get_numeric_data is includes _get_bool_data, so can't test for non-inclusion

def test_get_default(self):

# GH 7725
d0 = "a", "b", "c", "d"
d1 = np.arange(4, dtype='int64')
others = "e", 10

for data, index in ((d0, d1), (d1, d0)):
s = Series(data, index=index)
for i,d in zip(index, data):
self.assertEqual(s.get(i), d)
self.assertEqual(s.get(i, d), d)
self.assertEqual(s.get(i, "z"), d)
for other in others:
self.assertEqual(s.get(other, "z"), "z")
self.assertEqual(s.get(other, other), other)

def test_nonzero(self):

# GH 4633
Expand Down