Skip to content

Commit

Permalink
BUG: return NumPy scalars from Series, same speed as before, GH #510
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Dec 21, 2011
1 parent 9e6d49a commit 96375da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pandas/src/numpy_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ assign_value_1d(PyArrayObject* ap, Py_ssize_t _i, PyObject* v) {
char *item = (char *) PyArray_DATA(ap) + i * PyArray_STRIDE(ap, 0);
return PyArray_DESCR(ap)->f->setitem(v, item, ap);
}

PANDAS_INLINE PyObject*
get_value_1d(PyArrayObject* ap, Py_ssize_t i) {
char *item = (char *) PyArray_DATA(ap) + i * PyArray_STRIDE(ap, 0);
return PyArray_Scalar(item, PyArray_DESCR(ap), (PyObject*) ap);
}
8 changes: 4 additions & 4 deletions pandas/src/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cdef extern from "numpy_helper.h":
inline int is_float_object(object)
inline int is_bool_object(object)
inline int is_string_object(object)
inline int assign_value_1d (ndarray, Py_ssize_t, object) except -1

inline int assign_value_1d(ndarray, Py_ssize_t, object) except -1
inline object get_value_1d(ndarray, Py_ssize_t)

cdef inline object get_value_at(ndarray arr, object loc):
cdef:
Expand All @@ -24,8 +24,8 @@ cdef inline object get_value_at(ndarray arr, object loc):
i += sz
elif i >= sz:
raise IndexError('index out of bounds')
data_ptr = cnp.PyArray_GETPTR1(arr, i)
return cnp.PyArray_GETITEM(arr, data_ptr)

return get_value_1d(arr, i)

cdef inline set_value_at(ndarray arr, object loc, object value):
cdef:
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ def test_getitem_out_of_bounds(self):
# don't segfault, GH #495
self.assertRaises(IndexError, self.ts.__getitem__, len(self.ts))

def test_getitem_box_float64(self):
value = self.ts[5]
self.assert_(isinstance(value, np.float64))

def test_slice(self):
numSlice = self.series[10:20]
numSliceEnd = self.series[-10:]
Expand Down

0 comments on commit 96375da

Please sign in to comment.