Skip to content

BUG: float64index slicing with getitem bug #7504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,5 @@ Bug Fixes

- Bug in non-monotonic ``Index.union`` may preserve ``name`` incorrectly (:issue:`7458`)
- Bug in ``DatetimeIndex.intersection`` doesn't preserve timezone (:issue:`4690`)
- Bug in ``Float64Index`` slicing with ``__getitem__`` (``[]``) style indexing
(:issue:`7501`).
14 changes: 14 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import codecs
import csv
import types
from functools import partial
from datetime import datetime, timedelta

from numpy.lib.format import read_array, write_array
Expand All @@ -19,6 +20,7 @@
import pandas.tslib as tslib
from pandas import compat
from pandas.compat import StringIO, BytesIO, range, long, u, zip, map
import pandas.tools as tools

from pandas.core.config import get_option
from pandas.core import array as pa
Expand Down Expand Up @@ -2290,6 +2292,18 @@ def _is_sequence(x):
return False


def is_slice_of(typ_validator):
int_or_none = lambda x: x is None or typ_validator(x)
attrs = 'start', 'stop', 'step'
return lambda slc: all(map(tools.util.compose(int_or_none,
partial(getattr, slc)),
attrs))


is_int_slice = is_slice_of(is_integer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this routine exists already in core/indexing.py

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Pardon my wheel reinvention

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I think I had taken that out a while back. You can put it in core/indexing.py if you think its more generally useful. Though maybe better simply to handle this in convert_slice_indexer (in the Index one (e.g. the super)),




_ensure_float64 = algos.ensure_float64
_ensure_float32 = algos.ensure_float32
_ensure_int64 = algos.ensure_int64
Expand Down
8 changes: 3 additions & 5 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pandas.lib as lib
import pandas.algos as _algos
import pandas.index as _index
import pandas.tools as tools
from pandas.lib import Timestamp, is_datetime_array
from pandas.core.base import FrozenList, FrozenNDArray, IndexOpsMixin
from pandas.util.decorators import cache_readonly, deprecate
Expand Down Expand Up @@ -2004,13 +2005,10 @@ def _convert_scalar_indexer(self, key, typ=None):
def _convert_slice_indexer(self, key, typ=None):
""" convert a slice indexer, by definition these are labels
unless we are iloc """
if typ == 'iloc':
if typ == 'iloc' or (typ == 'getitem' and com.is_int_slice(key)):
return super(Float64Index, self)._convert_slice_indexer(key,
typ=typ)

# allow floats here
validator = lambda v: v is None or is_integer(v) or is_float(v)
self._validate_slicer(key, validator)
self._validate_slicer(key, lambda v: v is None or is_float(v))

# translate to locations
return self.slice_indexer(key.start, key.stop, key.step)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3629,6 +3629,16 @@ def test_duplicate_ix_returns_series(self):
e = df.loc[0.2, 'a']
tm.assert_series_equal(r, e)

def test_float_slice_ix_style(self):
n = 5
df = DataFrame(np.random.randn(n, 2), columns=list('ab'),
index=np.arange(0.0, n) + 50)
expected = df.iloc[2:4]
result = df[2:4]
tm.assert_frame_equal(expected, result)

assert df[2.0:4.0].empty


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down