Skip to content

Commit 7b58f1d

Browse files
committed
Merge pull request #3322 from jreback/index_astype
BUG: ensure index casting works even in Int64Index
2 parents d8070fa + f97e36a commit 7b58f1d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ pandas 0.11.0
293293
- fixed pretty priniting of sets (GH3294_)
294294
- Panel() and Panel.from_dict() now respects ordering when give OrderedDict (GH3303_)
295295
- DataFrame where with a datetimelike incorrectly selecting (GH3311_)
296+
- Ensure index casts work even in Int64Index
296297

297298
.. _GH3294: https://github.com/pydata/pandas/issues/3294
298299
.. _GH622: https://github.com/pydata/pandas/issues/622

pandas/core/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,11 @@ def inferred_type(self):
13321332
def _constructor(self):
13331333
return Int64Index
13341334

1335+
@cache_readonly
1336+
def _engine(self):
1337+
# property, for now, slow to look up
1338+
return self._engine_type(lambda: com._ensure_int64(self.values), len(self))
1339+
13351340
@property
13361341
def asi8(self):
13371342
# do not cache or you'll create a memory leak

pandas/tests/test_frame.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,17 @@ def test_set_index(self):
18361836
self.assertRaises(Exception, setattr, self.mixed_frame, 'index',
18371837
idx[::2])
18381838

1839+
def test_set_index_cast(self):
1840+
1841+
# issue casting an index then set_index
1842+
df = DataFrame({'A' : [1.1,2.2,3.3], 'B' : [5.0,6.1,7.2]},
1843+
index = [2010,2011,2012])
1844+
expected = df.ix[2010]
1845+
new_index = df.index.astype(np.int32)
1846+
df.index = new_index
1847+
result = df.ix[2010]
1848+
assert_series_equal(result,expected)
1849+
18391850
def test_set_index2(self):
18401851
df = DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar'],
18411852
'B': ['one', 'two', 'three', 'one', 'two'],

0 commit comments

Comments
 (0)