Repository: pandas-dev/pandas

Issue:
BUG: KeyErrors during value_counts
I'm getting occasional KeyErrors during `.value_counts()` on trunk.  It's pretty flaky, and this is the smallest example I had at hand (had one with only ~60 but lost it.)

```
>>> import pandas as pd
>>> print pd.__version__
0.12.0-1128-ge6aaf5e
>>> 
>>> ser = {256: 2321.0, 1: 78.0, 2: 2716.0, 3: 0.0, 4: 369.0, 5: 0.0, 6: 269.0, 7: 0.0, 8: 0.0, 9: 0.0, 10: 3536.0, 11: 0.0, 12: 24.0, 13: 0.0, 14: 931.0, 15: 0.0, 16: 101.0, 17: 78.0, 18: 9643.0, 19: 0.0, 20: 0.0, 21: 0.0, 22: 63761.0, 23: 0.0, 24: 446.0, 25: 0.0, 26: 34773.0, 27: 0.0, 28: 729.0, 29: 78.0, 30: 0.0, 31: 0.0, 32: 3374.0, 33: 0.0, 34: 1391.0, 35: 0.0, 36: 361.0, 37: 0.0, 38: 61808.0, 39: 0.0, 40: 0.0, 41: 0.0, 42: 6677.0, 43: 0.0, 44: 802.0, 45: 0.0, 46: 2691.0, 47: 0.0, 48: 3582.0, 49: 0.0, 50: 734.0, 51: 0.0, 52: 627.0, 53: 70.0, 54: 2584.0, 55: 0.0, 56: 324.0, 57: 0.0, 58: 605.0, 59: 0.0, 60: 0.0, 61: 0.0, 62: 3989.0, 63: 10.0, 64: 42.0, 65: 0.0, 66: 904.0, 67: 0.0, 68: 88.0, 69: 70.0, 70: 8172.0, 71: 0.0, 72: 0.0, 73: 0.0, 74: 64902.0, 75: 0.0, 76: 347.0, 77: 0.0, 78: 36605.0, 79: 0.0, 80: 379.0, 81: 70.0, 82: 0.0, 83: 0.0, 84: 3001.0, 85: 0.0, 86: 1630.0, 87: 7.0, 88: 364.0, 89: 0.0, 90: 67404.0, 91: 9.0, 92: 0.0, 93: 0.0, 94: 7685.0, 95: 0.0, 96: 1017.0, 97: 0.0, 98: 2831.0, 99: 0.0, 100: 2963.0, 101: 0.0, 102: 854.0, 103: 0.0, 104: 0.0, 105: 0.0, 106: 0.0, 107: 0.0, 108: 0.0, 109: 0.0, 110: 0.0, 111: 0.0, 112: 0.0, 113: 0.0, 114: 0.0, 115: 0.0, 116: 0.0, 117: 0.0, 118: 0.0, 119: 0.0, 120: 0.0, 121: 0.0, 122: 0.0, 123: 0.0, 124: 0.0, 125: 0.0, 126: 67744.0, 127: 22.0, 128: 264.0, 129: 0.0, 260: 197.0, 268: 0.0, 265: 0.0, 269: 0.0, 261: 0.0, 266: 1198.0, 267: 0.0, 262: 2629.0, 258: 775.0, 257: 0.0, 263: 0.0, 259: 0.0, 264: 163.0, 250: 10326.0, 251: 0.0, 252: 1228.0, 253: 0.0, 254: 2769.0, 255: 0.0}
>>> s = pd.Series(ser)
>>> s.value_counts()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/base.py", line 55, in __repr__
    return str(self)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/base.py", line 35, in __str__
    return self.__bytes__()
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/base.py", line 47, in __bytes__
    return self.__unicode__().encode(encoding, 'replace')
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/series.py", line 854, in __unicode__
    result = self._tidy_repr(min(30, max_rows - 4))
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/series.py", line 873, in _tidy_repr
    head = self[:num]._get_repr(print_header=True, length=False,
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/series.py", line 698, in __getslice__
    return self._slice(slobj)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/series.py", line 478, in _slice
    slobj = self.index._convert_slice_indexer(slobj, typ=typ or 'getitem')
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/index.py", line 1811, in _convert_slice_indexer
    return self.slice_indexer(key.start, key.stop, key.step)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/index.py", line 1493, in slice_indexer
    start_slice, end_slice = self.slice_locs(start, end)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/index.py", line 1555, in slice_locs
    end_slice = self.get_loc(end)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_1128_ge6aaf5e-py2.7-linux-i686.egg/pandas/core/index.py", line 1017, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "index.pyx", line 129, in pandas.index.IndexEngine.get_loc (pandas/index.c:3548)
  File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3428)
  File "hashtable.pyx", line 674, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:10784)
  File "hashtable.pyx", line 682, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:10737)
KeyError: 15
>>> 15 in s
True
>>> s[15]
0.0
```



Discussion:

Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -870,12 +870,12 @@ def _tidy_repr(self, max_vals=20):
         Internal function, should always return unicode string
         """
         num = max_vals // 2
-        head = self[:num]._get_repr(print_header=True, length=False,
-                                    dtype=False, name=False)
-        tail = self[-(max_vals - num):]._get_repr(print_header=False,
-                                                  length=False,
-                                                  name=False,
-                                                  dtype=False)
+        head = self.iloc[:num]._get_repr(print_header=True, length=False,
+                                         dtype=False, name=False)
+        tail = self.iloc[-(max_vals - num):]._get_repr(print_header=False,
+                                                       length=False,
+                                                       name=False,
+                                                       dtype=False)
         result = head + '\n...\n' + tail
         result = '%s\n%s' % (result, self._repr_footer())
 


Test patch:
diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py
--- a/pandas/tests/test_indexing.py
+++ b/pandas/tests/test_indexing.py
@@ -1797,6 +1797,15 @@ def f():
             df2["B"] = df2["A"]
             df2["B"] = df2["A"]
 
+    def test_float64index_slicing_bug(self):
+        # GH 5557, related to slicing a float index
+        ser = {256: 2321.0, 1: 78.0, 2: 2716.0, 3: 0.0, 4: 369.0, 5: 0.0, 6: 269.0, 7: 0.0, 8: 0.0, 9: 0.0, 10: 3536.0, 11: 0.0, 12: 24.0, 13: 0.0, 14: 931.0, 15: 0.0, 16: 101.0, 17: 78.0, 18: 9643.0, 19: 0.0, 20: 0.0, 21: 0.0, 22: 63761.0, 23: 0.0, 24: 446.0, 25: 0.0, 26: 34773.0, 27: 0.0, 28: 729.0, 29: 78.0, 30: 0.0, 31: 0.0, 32: 3374.0, 33: 0.0, 34: 1391.0, 35: 0.0, 36: 361.0, 37: 0.0, 38: 61808.0, 39: 0.0, 40: 0.0, 41: 0.0, 42: 6677.0, 43: 0.0, 44: 802.0, 45: 0.0, 46: 2691.0, 47: 0.0, 48: 3582.0, 49: 0.0, 50: 734.0, 51: 0.0, 52: 627.0, 53: 70.0, 54: 2584.0, 55: 0.0, 56: 324.0, 57: 0.0, 58: 605.0, 59: 0.0, 60: 0.0, 61: 0.0, 62: 3989.0, 63: 10.0, 64: 42.0, 65: 0.0, 66: 904.0, 67: 0.0, 68: 88.0, 69: 70.0, 70: 8172.0, 71: 0.0, 72: 0.0, 73: 0.0, 74: 64902.0, 75: 0.0, 76: 347.0, 77: 0.0, 78: 36605.0, 79: 0.0, 80: 379.0, 81: 70.0, 82: 0.0, 83: 0.0, 84: 3001.0, 85: 0.0, 86: 1630.0, 87: 7.0, 88: 364.0, 89: 0.0, 90: 67404.0, 91: 9.0, 92: 0.0, 93: 0.0, 94: 7685.0, 95: 0.0, 96: 1017.0, 97: 0.0, 98: 2831.0, 99: 0.0, 100: 2963.0, 101: 0.0, 102: 854.0, 103: 0.0, 104: 0.0, 105: 0.0, 106: 0.0, 107: 0.0, 108: 0.0, 109: 0.0, 110: 0.0, 111: 0.0, 112: 0.0, 113: 0.0, 114: 0.0, 115: 0.0, 116: 0.0, 117: 0.0, 118: 0.0, 119: 0.0, 120: 0.0, 121: 0.0, 122: 0.0, 123: 0.0, 124: 0.0, 125: 0.0, 126: 67744.0, 127: 22.0, 128: 264.0, 129: 0.0, 260: 197.0, 268: 0.0, 265: 0.0, 269: 0.0, 261: 0.0, 266: 1198.0, 267: 0.0, 262: 2629.0, 258: 775.0, 257: 0.0, 263: 0.0, 259: 0.0, 264: 163.0, 250: 10326.0, 251: 0.0, 252: 1228.0, 253: 0.0, 254: 2769.0, 255: 0.0}
+
+        # smoke test for the repr
+        s = Series(ser)
+        result  = s.value_counts()
+        str(result)
+
     def test_floating_index_doc_example(self):
 
         index = Index([1.5, 2, 3, 4.5, 5])