>> s a 1 b 2 c 3 dtype: int64 >>> s.get("d", 0) 0 ...">

BUG(?): get default value doesn't work for Series · Issue #7725 · pandas-dev/pandas (original) (raw)

Series have a get method, but using the default value doesn't work for certain type combinations:

>>> s = pd.Series([1,2,3], index=["a","b","c"])
>>> s
a    1
b    2
c    3
dtype: int64
>>> s.get("d", 0)
0
>>> s.get(10, 0)
Traceback (most recent call last):
  File "<ipython-input-18-26d73ac73179>", line 1, in <module>
    s.get(10, 0)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_421_g20dfc6b-py2.7-linux-x86_64.egg/pandas/core/generic.py", line 1040, in get
    return self[key]
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_421_g20dfc6b-py2.7-linux-x86_64.egg/pandas/core/series.py", line 484, in __getitem__
    result = self.index.get_value(self, key)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_421_g20dfc6b-py2.7-linux-x86_64.egg/pandas/core/index.py", line 1202, in get_value
    return tslib.get_value_box(s, key)
  File "tslib.pyx", line 540, in pandas.tslib.get_value_box (pandas/tslib.c:11831)
  File "tslib.pyx", line 555, in pandas.tslib.get_value_box (pandas/tslib.c:11678)
IndexError: index out of bounds

I'm not sure whether it makes the most sense just to teach .get to catch IndexErrors as well as KeyErrors and ValueErrors (which is what it does now), or whether a deeper change is warranted.