From 4f33a81e2fa09cfc03162245936518626e4ee550 Mon Sep 17 00:00:00 2001 From: Aleks Ch Date: Sun, 2 Sep 2018 19:13:36 +0300 Subject: [PATCH 1/4] BUG: fix failing DataFrame.loc with IntervalIndex --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/indexing.py | 2 +- pandas/tests/frame/test_indexing.py | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 1979bde796452..fb675ae7ba849 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -669,6 +669,7 @@ Indexing - Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`) - Bug where mixed indexes wouldn't allow integers for ``.at`` (:issue:`19860`) - ``Float64Index.get_loc`` now raises ``KeyError`` when boolean key passed. (:issue:`19087`) +- Bug when asking ``.loc`` for :class:`DataFrame` with :class:`IntervalIndex` index. (:issue:`19977`) Missing ^^^^^^^ diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index a245ecfa007f3..b63f874abff85 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1491,7 +1491,7 @@ def __getitem__(self, key): try: if self._is_scalar_access(key): return self._getitem_scalar(key) - except (KeyError, IndexError): + except (KeyError, IndexError, AttributeError): pass return self._getitem_tuple(key) else: diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index 6a4cf1ffc6071..c0e68d9e24d99 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3099,6 +3099,26 @@ def test_type_error_multiindex(self): result = dg['x', 0] assert_series_equal(result, expected) + def test_interval_index(self): + index = pd.interval_range(start=0, periods=3) + df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], + index=index, + columns=['A', 'B', 'C']) + + expected = 1 + result = df.loc[0.5, 'A'] + assert_almost_equal(result, expected) + + index = pd.interval_range(start=0, periods=3, closed='both') + df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], + index=index, + columns=['A', 'B', 'C']) + + index_exp = pd.interval_range(start=0, periods=2, freq=1, closed='both') + expected = pd.Series([1, 4], index=index_exp, name='A') + result = df.loc[1, 'A'] + assert_series_equal(result, expected) + class TestDataFrameIndexingDatetimeWithTZ(TestData): From acd80470dc2a22dbc9c4ca7ec42b3687007f189e Mon Sep 17 00:00:00 2001 From: Aleks Ch Date: Sun, 2 Sep 2018 19:40:45 +0300 Subject: [PATCH 2/4] BUG: fix failing DataFrame.loc with IntervalIndex --- pandas/tests/frame/test_indexing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index c0e68d9e24d99..a66fe44c4b603 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3114,7 +3114,8 @@ def test_interval_index(self): index=index, columns=['A', 'B', 'C']) - index_exp = pd.interval_range(start=0, periods=2, freq=1, closed='both') + index_exp = pd.interval_range(start=0, periods=2, + freq=1, closed='both') expected = pd.Series([1, 4], index=index_exp, name='A') result = df.loc[1, 'A'] assert_series_equal(result, expected) From 37b9352ad7ead0195dc2391ea9634b0e52399753 Mon Sep 17 00:00:00 2001 From: Aleks Ch Date: Sun, 2 Sep 2018 19:58:32 +0300 Subject: [PATCH 3/4] BUG: fix failing DataFrame.loc with IntervalIndex --- pandas/tests/frame/test_indexing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index a66fe44c4b603..f0c4d7be2f293 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3100,6 +3100,7 @@ def test_type_error_multiindex(self): assert_series_equal(result, expected) def test_interval_index(self): + # GH 19977 index = pd.interval_range(start=0, periods=3) df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=index, From 0888647359902b18f07bb6255bf02ce5f8d3ebe6 Mon Sep 17 00:00:00 2001 From: Aleks Ch Date: Mon, 3 Sep 2018 21:52:03 +0300 Subject: [PATCH 4/4] fix whatsnew entry --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index fb675ae7ba849..3770e130e4dad 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -669,7 +669,7 @@ Indexing - Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`) - Bug where mixed indexes wouldn't allow integers for ``.at`` (:issue:`19860`) - ``Float64Index.get_loc`` now raises ``KeyError`` when boolean key passed. (:issue:`19087`) -- Bug when asking ``.loc`` for :class:`DataFrame` with :class:`IntervalIndex` index. (:issue:`19977`) +- Bug in :meth:`DataFrame.loc` when indexing with an :class:`IntervalIndex` (:issue:`19977`) Missing ^^^^^^^