Skip to content

Commit 6a481e5

Browse files
authored
GH-48463: [Python] Improve error message in CheckTypeExact arrow_to_pandas.cc (#48464)
### Rationale for this change 3b000b7 first introduced this todo as below https://github.com/apache/arrow/blob/0bfbd19bce3e10163537b349f9205b635c87eea7/python/pyarrow/src/arrow/python/arrow_to_pandas.cc#L1130 Then, the error messages are actually handled at the higher level if I am not mistaken (e.g., 05bc63c introduced `_sanitize_arrays` and casting at `asarray` ). So the end users would not reach this. This is more for a sanity check and developers to debug. This is to improve the error message for the sanity check and developers. ### What changes are included in this PR? This PR improves the error message in CheckTypeExact arrow_to_pandas.cc by adding the corresponding NumPy string type. ### Are these changes tested? Unittest is added, and manually run as below: ``` pytest pyarrow/tests/test_cpp_internals.py::test_get_numpy_type_name -v ``` ### Are there any user-facing changes? No. * GitHub Issue: #48463 Authored-by: Hyukjin Kwon <[email protected]> Signed-off-by: Rok Mihevc <[email protected]>
1 parent 71d4931 commit 6a481e5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

python/pyarrow/src/arrow/python/arrow_to_pandas.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,9 @@ class TypedPandasWriter : public PandasWriter {
11271127

11281128
Status CheckTypeExact(const DataType& type, Type::type expected) {
11291129
if (type.id() != expected) {
1130-
// TODO(wesm): stringify NumPy / pandas type
1131-
return Status::NotImplemented("Cannot write Arrow data of type ", type.ToString());
1130+
return Status::NotImplemented("Cannot write Arrow data of type ", type.ToString(),
1131+
" to pandas block with NumPy type ",
1132+
GetNumPyTypeName(NPY_TYPE));
11321133
}
11331134
return Status::OK();
11341135
}

python/pyarrow/src/arrow/python/python_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "arrow/python/decimal.h"
3333
#include "arrow/python/helpers.h"
3434
#include "arrow/python/numpy_convert.h"
35+
#include "arrow/python/numpy_internal.h"
3536
#include "arrow/python/numpy_interop.h"
3637
#include "arrow/python/python_test.h"
3738
#include "arrow/python/python_to_arrow.h"
@@ -847,6 +848,21 @@ Status TestUpdateWithNaN() {
847848
return Status::OK();
848849
}
849850

851+
Status TestGetNumPyTypeName() {
852+
ASSERT_EQ(GetNumPyTypeName(NPY_BOOL), "bool");
853+
ASSERT_EQ(GetNumPyTypeName(NPY_INT8), "int8");
854+
ASSERT_EQ(GetNumPyTypeName(NPY_INT16), "int16");
855+
ASSERT_EQ(GetNumPyTypeName(NPY_INT32), "int32");
856+
ASSERT_EQ(GetNumPyTypeName(NPY_INT64), "int64");
857+
ASSERT_EQ(GetNumPyTypeName(NPY_UINT8), "uint8");
858+
ASSERT_EQ(GetNumPyTypeName(NPY_UINT16), "uint16");
859+
ASSERT_EQ(GetNumPyTypeName(NPY_UINT32), "uint32");
860+
ASSERT_EQ(GetNumPyTypeName(NPY_UINT64), "uint64");
861+
ASSERT_EQ(GetNumPyTypeName(NPY_FLOAT32), "float32");
862+
ASSERT_EQ(GetNumPyTypeName(NPY_FLOAT64), "float64");
863+
return Status::OK();
864+
}
865+
850866
} // namespace
851867

852868
std::vector<TestCase> GetCppTestCases() {
@@ -886,6 +902,7 @@ std::vector<TestCase> GetCppTestCases() {
886902
TestMixedPrecisionAndScaleSequenceConvert},
887903
{"test_simple_inference", TestSimpleInference},
888904
{"test_update_with_nan", TestUpdateWithNaN},
905+
{"test_get_numpy_type_name", TestGetNumPyTypeName},
889906
};
890907
}
891908

0 commit comments

Comments
 (0)