Skip to content

Commit 32d0d1b

Browse files
committed
BUG: allow get default value upon IndexError, GH #7725
1 parent 5adb0b6 commit 32d0d1b

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/v0.15.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ There are no experimental changes in 0.15.0
152152
Bug Fixes
153153
~~~~~~~~~
154154

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

156157

157158

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ def get(self, key, default=None):
10381038
"""
10391039
try:
10401040
return self[key]
1041-
except (KeyError, ValueError):
1041+
except (KeyError, ValueError, IndexError):
10421042
return default
10431043

10441044
def __getitem__(self, item):

pandas/tests/test_generic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ def test_get_numeric_data(self):
123123

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

126+
def test_get_default(self):
127+
128+
# GH 7725
129+
d0 = "a", "b", "c", "d"
130+
d1 = np.arange(4, dtype='int64')
131+
others = "e", 10
132+
133+
for data, index in ((d0, d1), (d1, d0)):
134+
s = Series(data, index=index)
135+
for i,d in zip(index, data):
136+
self.assertEqual(s.get(i), d)
137+
self.assertEqual(s.get(i, d), d)
138+
self.assertEqual(s.get(i, "z"), d)
139+
for other in others:
140+
self.assertEqual(s.get(other, "z"), "z")
141+
self.assertEqual(s.get(other, other), other)
142+
126143
def test_nonzero(self):
127144

128145
# GH 4633

0 commit comments

Comments
 (0)