Skip to content

Commit f403f9d

Browse files
committed
Fix test case to be platform independent, note ARROW-1345. Improve quality of error message
Change-Id: I3bef642f3620396ea728b54fefe780b915d8fc32
1 parent f4f44c1 commit f403f9d

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

cpp/src/arrow/python/numpy-internal.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,41 @@ class Ndarray1DIndexer {
6464
int64_t stride_;
6565
};
6666

67+
static inline std::string GetNumPyTypeName(int npy_type) {
68+
#define TYPE_CASE(TYPE, NAME) \
69+
case NPY_##TYPE: \
70+
return NAME;
71+
72+
switch (npy_type) {
73+
TYPE_CASE(BOOL, "bool")
74+
TYPE_CASE(INT8, "int8")
75+
TYPE_CASE(INT16, "int16")
76+
TYPE_CASE(INT32, "int32")
77+
TYPE_CASE(INT64, "int64")
78+
#if (NPY_INT64 != NPY_LONGLONG)
79+
TYPE_CASE(LONGLONG, "longlong")
80+
#endif
81+
TYPE_CASE(UINT8, "uint8")
82+
TYPE_CASE(UINT16, "uint16")
83+
TYPE_CASE(UINT32, "uint32")
84+
TYPE_CASE(UINT64, "uint64")
85+
#if (NPY_UINT64 != NPY_ULONGLONG)
86+
TYPE_CASE(ULONGLONG, "ulonglong")
87+
#endif
88+
TYPE_CASE(FLOAT16, "float16")
89+
TYPE_CASE(FLOAT32, "float32")
90+
TYPE_CASE(FLOAT64, "float64")
91+
TYPE_CASE(DATETIME, "datetime64")
92+
TYPE_CASE(OBJECT, "object")
93+
TYPE_CASE(VOID, "void")
94+
default:
95+
break;
96+
}
97+
98+
#undef TYPE_CASE
99+
return "unrecognized type in GetNumPyTypeName";
100+
}
101+
67102
} // namespace py
68103
} // namespace arrow
69104

cpp/src/arrow/python/pandas_to_arrow.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ Status CheckFlatNumpyArray(PyArrayObject* numpy_array, int np_type) {
151151
return Status::Invalid("only handle 1-dimensional arrays");
152152
}
153153

154-
if (PyArray_DESCR(numpy_array)->type_num != np_type) {
155-
return Status::Invalid("can only handle exact conversions");
154+
const int received_type = PyArray_DESCR(numpy_array)->type_num;
155+
if (received_type != np_type) {
156+
std::stringstream ss;
157+
ss << "trying to convert NumPy type " << GetNumPyTypeName(np_type) << " but got "
158+
<< GetNumPyTypeName(received_type);
159+
return Status::Invalid(ss.str());
156160
}
157161

158162
return Status::OK();

python/pyarrow/error.pxi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cdef int check_status(const CStatus& status) nogil except -1:
6565
return 0
6666

6767
with gil:
68-
message = frombytes(status.ToString())
68+
message = frombytes(status.message())
6969
if status.IsInvalid():
7070
raise ArrowInvalid(message)
7171
elif status.IsIOError():
@@ -85,4 +85,5 @@ cdef int check_status(const CStatus& status) nogil except -1:
8585
elif status.IsPlasmaStoreFull():
8686
raise PlasmaStoreFull(message)
8787
else:
88+
message = frombytes(status.ToString())
8889
raise ArrowException(message)

python/pyarrow/includes/common.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
4242
CStatus()
4343

4444
c_string ToString()
45+
c_string message()
4546

4647
c_bool ok()
4748
c_bool IsIOError()

python/pyarrow/tests/pandas_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def dataframe_with_lists(include_index=False):
9999
[0, 1, 2, 3, 4],
100100
None,
101101
[0],
102-
np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9] * 2)[::2]
102+
np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9] * 2,
103+
dtype=np.int64)[::2]
103104
]
104105
fields.append(pa.field('double', pa.list_(pa.float64())))
105106
arrays['double'] = [

0 commit comments

Comments
 (0)