From 27555e7e970d107da7a0a8988bfcd8ae393e1033 Mon Sep 17 00:00:00 2001 From: jreback Date: Mon, 9 Jun 2014 09:00:51 -0400 Subject: [PATCH 1/2] BUG: Bug in multi-index slicing with incomplete indexers (GH7399) --- doc/source/v0.14.1.txt | 2 +- pandas/core/index.py | 4 ++-- pandas/tests/test_indexing.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index 586e47ff4f303..0e668f3e83e56 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -160,7 +160,7 @@ Bug Fixes - +- Bug in multi-index slicing with incomplete indexers (:issue:`7399`) diff --git a/pandas/core/index.py b/pandas/core/index.py index fbadd92c1329c..1069f7df4d879 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -3526,8 +3526,8 @@ def _get_level_indexer(self, key, level=0): # handle a slice, returnig a slice if we can # otherwise a boolean indexer - start = level_index.get_loc(key.start) - stop = level_index.get_loc(key.stop) + start = level_index.get_loc(key.start or 0) + stop = level_index.get_loc(key.stop or len(level_index)-1) step = key.step if level > 0 or self.lexsort_depth == 0: diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 7610ccc6cdf73..e11ccec224742 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -1325,6 +1325,18 @@ def test_loc_multiindex(self): result = df.loc[[1,2]] assert_frame_equal(result, expected) + # GH 7399 + # incomplete indexers + s = pd.Series(np.arange(15,dtype='int64'),MultiIndex.from_product([range(5), ['a', 'b', 'c']])) + expected = s.loc[:, 'a':'c'] + result = s.loc[0:4, 'a':'c'] + assert_series_equal(result, expected) + + result = s.loc[:4, 'a':'c'] + assert_series_equal(result, expected) + + result = s.loc[0:, 'a':'c'] + assert_series_equal(result, expected) def test_series_getitem_multiindex(self): From c5788939c9ff6852da5585144fa4cbe2096f3411 Mon Sep 17 00:00:00 2001 From: jreback Date: Mon, 9 Jun 2014 09:17:58 -0400 Subject: [PATCH 2/2] BUG: Bug in multi-index slicing with a step in a sliced level (GH7400) --- doc/source/v0.14.1.txt | 8 +++----- pandas/core/index.py | 2 +- pandas/tests/test_indexing.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index 0e668f3e83e56..1c564fbf76f59 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -160,11 +160,6 @@ Bug Fixes -- Bug in multi-index slicing with incomplete indexers (:issue:`7399`) - - - -- Bug in ``.ix`` getitem should always return a Series (:issue:`7150`) @@ -216,3 +211,6 @@ Bug Fixes (:issue:`7366`). - Bug where ``NDFrame.replace()`` didn't correctly replace objects with ``Period`` values (:issue:`7379`). +- Bug in ``.ix`` getitem should always return a Series (:issue:`7150`) +- Bug in multi-index slicing with incomplete indexers (:issue:`7399`) +- Bug in multi-index slicing with a step in a sliced level (:issue:`7400`) diff --git a/pandas/core/index.py b/pandas/core/index.py index 1069f7df4d879..69edf8d9c3f42 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -3530,7 +3530,7 @@ def _get_level_indexer(self, key, level=0): stop = level_index.get_loc(key.stop or len(level_index)-1) step = key.step - if level > 0 or self.lexsort_depth == 0: + if level > 0 or self.lexsort_depth == 0 or step is not None: # need to have like semantics here to right # searching as when we are using a slice # so include the stop+1 (so we include stop) diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index e11ccec224742..1945236f4efe8 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -1329,14 +1329,25 @@ def test_loc_multiindex(self): # incomplete indexers s = pd.Series(np.arange(15,dtype='int64'),MultiIndex.from_product([range(5), ['a', 'b', 'c']])) expected = s.loc[:, 'a':'c'] + result = s.loc[0:4, 'a':'c'] assert_series_equal(result, expected) + assert_series_equal(result, expected) result = s.loc[:4, 'a':'c'] assert_series_equal(result, expected) + assert_series_equal(result, expected) result = s.loc[0:, 'a':'c'] assert_series_equal(result, expected) + assert_series_equal(result, expected) + + # GH 7400 + # multiindexer gettitem with list of indexers skips wrong element + s = pd.Series(np.arange(15,dtype='int64'),MultiIndex.from_product([range(5), ['a', 'b', 'c']])) + expected = s.iloc[[6,7,8,12,13,14]] + result = s.loc[2:4:2, 'a':'c'] + assert_series_equal(result, expected) def test_series_getitem_multiindex(self):