Skip to content

Commit b3b5140

Browse files
committed
BUG: fix integer-slicing from integers-as-floats, GH #670
1 parent e66d25e commit b3b5140

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

pandas/core/indexing.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _convert_to_indexer(self, obj, axis=0):
220220
is_int_index = _is_integer_index(labels)
221221
if isinstance(obj, slice):
222222

223-
int_slice = _is_integer_slice(obj)
223+
int_slice = _is_index_slice(obj)
224224
null_slice = obj.start is None and obj.stop is None
225225
# could have integers in the first level of the MultiIndex
226226
position_slice = (int_slice
@@ -234,7 +234,7 @@ def _convert_to_indexer(self, obj, axis=0):
234234
i, j = labels.slice_locs(obj.start, obj.stop)
235235
slicer = slice(i, j, obj.step)
236236
except Exception:
237-
if _is_integer_slice(obj):
237+
if _is_index_slice(obj):
238238
if labels.inferred_type == 'integer':
239239
raise
240240
slicer = obj
@@ -276,7 +276,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
276276
axis_name = obj._get_axis_name(axis)
277277
labels = getattr(obj, axis_name)
278278

279-
int_slice = _is_integer_slice(slice_obj)
279+
int_slice = _is_index_slice(slice_obj)
280280

281281
null_slice = slice_obj.start is None and slice_obj.stop is None
282282
# could have integers in the first level of the MultiIndex
@@ -289,7 +289,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
289289
i, j = labels.slice_locs(slice_obj.start, slice_obj.stop)
290290
slicer = slice(i, j, slice_obj.step)
291291
except Exception:
292-
if _is_integer_slice(slice_obj):
292+
if _is_index_slice(slice_obj):
293293
if labels.inferred_type == 'integer':
294294
raise
295295
slicer = slice_obj
@@ -301,9 +301,12 @@ def _get_slice_axis(self, slice_obj, axis=0):
301301

302302
return self._slice(slicer, axis=axis)
303303

304-
def _is_integer_slice(obj):
304+
def _is_index_slice(obj):
305+
def _is_valid_index(x):
306+
return com.is_integer(x) or com.is_float(x) and np.allclose(x, int(x))
307+
305308
def _crit(v):
306-
return v is None or com.is_integer(v)
309+
return v is None or _is_valid_index(v)
307310

308311
both_none = obj.start is None and obj.stop is None
309312

pandas/tests/test_series.py

+11
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ def test_slice_can_reorder_not_uniquely_indexed(self):
483483
s = Series(1, index=['a', 'a', 'b', 'b', 'c'])
484484
result = s[::-1] # it works!
485485

486+
def test_slice_float_get_set(self):
487+
result = self.ts[4.0:10.0]
488+
expected = self.ts[4:10]
489+
assert_series_equal(result, expected)
490+
491+
self.ts[4.0:10.0] = 0
492+
self.assert_((self.ts[4:10] == 0).all())
493+
494+
self.assertRaises(TypeError, self.ts.__getitem__, slice(4.5, 10.0))
495+
self.assertRaises(TypeError, self.ts.__setitem__, slice(4.5, 10.0), 0)
496+
486497
def test_setitem(self):
487498
self.ts[self.ts.index[5]] = np.NaN
488499
self.ts[[1,2,17]] = np.NaN

0 commit comments

Comments
 (0)