Skip to content

Commit bc1932f

Browse files
committed
BUG: fix additional float-based slicing issue, with test
1 parent 35fcc17 commit bc1932f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

pandas/core/indexing.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,27 @@ def _get_slice_axis(self, slice_obj, axis=0):
292292

293293
int_slice = _is_index_slice(slice_obj)
294294

295+
start = slice_obj.start
296+
stop = slice_obj.stop
297+
298+
# in case of providing all floats, use label-based indexing
299+
float_slice = (labels.inferred_type == 'floating'
300+
and (type(start) == float or start is None)
301+
and (type(stop) == float or stop is None))
302+
295303
null_slice = slice_obj.start is None and slice_obj.stop is None
296-
# could have integers in the first level of the MultiIndex
304+
305+
# could have integers in the first level of the MultiIndex, in which
306+
# case we wouldn't want to do position-based slicing
297307
position_slice = (int_slice and not labels.inferred_type == 'integer'
298-
and not isinstance(labels, MultiIndex))
308+
and not isinstance(labels, MultiIndex)
309+
and not float_slice)
310+
299311
if null_slice or position_slice:
300312
slicer = slice_obj
301313
else:
302314
try:
303-
i, j = labels.slice_locs(slice_obj.start, slice_obj.stop)
315+
i, j = labels.slice_locs(start, stop)
304316
slicer = slice(i, j, slice_obj.step)
305317
except Exception:
306318
if _is_index_slice(slice_obj):

pandas/tests/test_series.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ def test_slice_float_get_set(self):
526526
self.assertRaises(TypeError, self.ts.__getitem__, slice(4.5, 10.0))
527527
self.assertRaises(TypeError, self.ts.__setitem__, slice(4.5, 10.0), 0)
528528

529+
def test_slice_floats2(self):
530+
s = Series(np.random.rand(10), index=np.arange(10,20,dtype=float))
531+
532+
self.assert_(len(s.ix[12.0:] == 8))
533+
self.assert_(len(s.ix[12.5:] == 7))
534+
535+
i = np.arange(10,20,dtype=float)
536+
i[2] = 12.2
537+
s.index = i
538+
self.assert_(len(s.ix[12.0:] == 8))
539+
self.assert_(len(s.ix[12.5:] == 7))
540+
529541
def test_setitem(self):
530542
self.ts[self.ts.index[5]] = np.NaN
531543
self.ts[[1,2,17]] = np.NaN

0 commit comments

Comments
 (0)