From f97e36ad04d3b12d8dfe2e650bada5b849859b5a Mon Sep 17 00:00:00 2001 From: jreback Date: Thu, 11 Apr 2013 17:46:23 -0400 Subject: [PATCH] BUG: ensure index casting works even in Int64Index --- RELEASE.rst | 1 + pandas/core/index.py | 5 +++++ pandas/tests/test_frame.py | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/RELEASE.rst b/RELEASE.rst index a542a406fcfaa..9016a9efdcae3 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -293,6 +293,7 @@ pandas 0.11.0 - fixed pretty priniting of sets (GH3294_) - Panel() and Panel.from_dict() now respects ordering when give OrderedDict (GH3303_) - DataFrame where with a datetimelike incorrectly selecting (GH3311_) + - Ensure index casts work even in Int64Index .. _GH3294: https://github.com/pydata/pandas/issues/3294 .. _GH622: https://github.com/pydata/pandas/issues/622 diff --git a/pandas/core/index.py b/pandas/core/index.py index aa0fd5b6b0351..5b10a2321a387 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -1332,6 +1332,11 @@ def inferred_type(self): def _constructor(self): return Int64Index + @cache_readonly + def _engine(self): + # property, for now, slow to look up + return self._engine_type(lambda: com._ensure_int64(self.values), len(self)) + @property def asi8(self): # do not cache or you'll create a memory leak diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 5e5b24bb6734d..de0011185e35b 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -1836,6 +1836,17 @@ def test_set_index(self): self.assertRaises(Exception, setattr, self.mixed_frame, 'index', idx[::2]) + def test_set_index_cast(self): + + # issue casting an index then set_index + df = DataFrame({'A' : [1.1,2.2,3.3], 'B' : [5.0,6.1,7.2]}, + index = [2010,2011,2012]) + expected = df.ix[2010] + new_index = df.index.astype(np.int32) + df.index = new_index + result = df.ix[2010] + assert_series_equal(result,expected) + def test_set_index2(self): df = DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar'], 'B': ['one', 'two', 'three', 'one', 'two'],