Skip to content

Commit

Permalink
Merge pull request pandas-dev#3322 from jreback/index_astype
Browse files Browse the repository at this point in the history
BUG: ensure index casting works even in Int64Index
  • Loading branch information
wesm committed Apr 12, 2013
2 parents d8070fa + f97e36a commit 7b58f1d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 7b58f1d

Please sign in to comment.