Skip to content

Commit

Permalink
BUG: don't accidentally upcast int->float in .ix scalar indexing, GH #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Dec 2, 2011
1 parent c6d05e5 commit ec1eebc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pandas/core/frame.py
Expand Up @@ -921,7 +921,9 @@ def get_value(self, index, col):
"""
iloc = self.index.get_loc(index)
vals = self._getitem_single(col).values
return vals[iloc]
result = vals[iloc]
assert(not lib.is_array(result)) # a little faster than isinstance
return result

def put_value(self, index, col, value):
"""
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexing.py
Expand Up @@ -25,6 +25,11 @@ def __init__(self, obj):

def __getitem__(self, key):
if isinstance(key, tuple):
try:
return self.obj.get_value(*key)
except Exception:
pass

return self._getitem_tuple(key)
else:
return self._getitem_axis(key, axis=0)
Expand Down
2 changes: 2 additions & 0 deletions pandas/src/tseries.pyx
Expand Up @@ -469,6 +469,8 @@ cdef class cache_readonly(object):
PyDict_SetItem(cache, self.name, val)
return val

cpdef is_array(object o):
return np.PyArray_Check(o)

include "skiplist.pyx"
include "groupby.pyx"
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/test_frame.py
Expand Up @@ -674,6 +674,13 @@ def test_put_value(self):
self.frame.put_value(idx, col, 1)
self.assertEqual(self.frame[col][idx], 1)

def test_single_element_ix_dont_upcast(self):
self.frame['E'] = 1
self.assert_(issubclass(self.frame['E'].dtype.type, np.integer))

result = self.frame.ix[self.frame.index[5], 'E']
self.assert_(isinstance(result, np.integer))

_seriesd = tm.getSeriesData()
_tsd = tm.getTimeSeriesData()

Expand Down Expand Up @@ -2805,7 +2812,6 @@ def test_apply_reduce_Series(self):
expected = self.frame.mean(1)
assert_series_equal(result, expected)


def test_applymap(self):
applied = self.frame.applymap(lambda x: x * 2)
assert_frame_equal(applied, self.frame * 2)
Expand Down

0 comments on commit ec1eebc

Please sign in to comment.