Skip to content

Commit d67f140

Browse files
committed
BUG: unbox 0-dimensional arrays in map_infer, GH #690
1 parent 628c029 commit d67f140

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

pandas/src/inference.pyx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ def infer_dtype_list(list values):
8383
cdef inline bint is_datetime(object o):
8484
return PyDateTime_Check(o)
8585

86-
cpdef is_array(object o):
87-
return np.PyArray_Check(o)
88-
89-
9086
def is_bool_array(ndarray values):
9187
cdef:
9288
Py_ssize_t i, n = len(values)
@@ -401,8 +397,16 @@ def map_infer(ndarray arr, object f):
401397
n = len(arr)
402398
result = np.empty(n, dtype=object)
403399
for i in range(n):
404-
val = PyArray_GETITEM(arr, PyArray_ITER_DATA(it))
405-
result[i] = f(val)
400+
val = f(PyArray_GETITEM(arr, PyArray_ITER_DATA(it)))
401+
402+
# unbox 0-dim arrays, GH #690
403+
if is_array(val) and PyArray_NDIM(val) == 0:
404+
# is there a faster way to unbox?
405+
val = val.item()
406+
407+
result[i] = val
408+
409+
406410
PyArray_ITER_NEXT(it)
407411

408412
return maybe_convert_objects(result, try_float=0)

pandas/src/tseries.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cdef inline int int_min(int a, int b): return a if a <= b else b
2424
ctypedef unsigned char UChar
2525

2626
cimport util
27+
from util cimport is_array
2728

2829
cdef extern from "math.h":
2930
double sqrt(double x)

pandas/src/util.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ cdef inline set_value_at(ndarray arr, object loc, object value):
4949
cdef inline int is_contiguous(ndarray arr):
5050
return cnp.PyArray_CHKFLAGS(arr, cnp.NPY_C_CONTIGUOUS)
5151

52+
cdef inline is_array(object o):
53+
return cnp.PyArray_Check(o)

pandas/tests/test_series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,11 @@ def test_map_int(self):
16671667
self.assert_(isnull(merged['d']))
16681668
self.assert_(not isnull(merged['c']))
16691669

1670+
def test_map_type_inference(self):
1671+
s = Series(range(3))
1672+
s2 = s.map(lambda x: np.where(x == 0, 0, 1))
1673+
self.assert_(issubclass(s2.dtype.type, np.integer))
1674+
16701675
def test_map_decimal(self):
16711676
from decimal import Decimal
16721677

0 commit comments

Comments
 (0)