diff --git a/cpp/src/arrow/python/CMakeLists.txt b/cpp/src/arrow/python/CMakeLists.txt index 03f5afc624b3..faaad89656f9 100644 --- a/cpp/src/arrow/python/CMakeLists.txt +++ b/cpp/src/arrow/python/CMakeLists.txt @@ -88,6 +88,6 @@ install(FILES # INSTALL_RPATH "\$ORIGIN") if (ARROW_BUILD_TESTS) - ADD_ARROW_TEST(pandas-test + ADD_ARROW_TEST(python-test STATIC_LINK_LIBS "${ARROW_PYTHON_TEST_LINK_LIBS}") endif() diff --git a/cpp/src/arrow/python/pandas_convert.cc b/cpp/src/arrow/python/pandas_convert.cc index 68a8d7d7afcf..ae9b17ca9ac8 100644 --- a/cpp/src/arrow/python/pandas_convert.cc +++ b/cpp/src/arrow/python/pandas_convert.cc @@ -46,6 +46,7 @@ #include "arrow/type_fwd.h" #include "arrow/type_traits.h" #include "arrow/util/bit-util.h" +#include "arrow/util/logging.h" #include "arrow/util/macros.h" namespace arrow { @@ -167,8 +168,10 @@ Status AppendObjectStrings(int64_t objects_length, StringBuilder* builder, *have_bytes = true; const int32_t length = static_cast(PyBytes_GET_SIZE(obj)); RETURN_NOT_OK(builder->Append(PyBytes_AS_STRING(obj), length)); + } else if (PyObject_is_null(obj)) { + RETURN_NOT_OK(builder->AppendNull()); } else { - builder->AppendNull(); + return InvalidConversion(obj, "string or bytes"); } } @@ -197,8 +200,10 @@ static Status AppendObjectFixedWidthBytes(int64_t objects_length, int byte_width RETURN_NOT_OK(CheckPythonBytesAreFixedLength(obj, byte_width)); RETURN_NOT_OK( builder->Append(reinterpret_cast(PyBytes_AS_STRING(obj)))); + } else if (PyObject_is_null(obj)) { + RETURN_NOT_OK(builder->AppendNull()); } else { - builder->AppendNull(); + return InvalidConversion(obj, "string or bytes"); } } @@ -413,6 +418,32 @@ inline Status PandasConverter::ConvertData(std::shared_ptr* return Status::OK(); } +Status InvalidConversion(PyObject* obj, const std::string& expected_type_name) { + OwnedRef type(PyObject_Type(obj)); + RETURN_IF_PYERROR(); + DCHECK_NE(type.obj(), nullptr); + + OwnedRef type_name(PyObject_GetAttrString(type.obj(), "__name__")); + RETURN_IF_PYERROR(); + DCHECK_NE(type_name.obj(), nullptr); + + OwnedRef bytes_obj(PyUnicode_AsUTF8String(type_name.obj())); + RETURN_IF_PYERROR(); + DCHECK_NE(bytes_obj.obj(), nullptr); + + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj.obj()); + const char* bytes = PyBytes_AS_STRING(bytes_obj.obj()); + + DCHECK_NE(bytes, nullptr) << "bytes from type(...).__name__ were null"; + + std::string cpp_type_name(bytes, size); + + std::stringstream ss; + ss << "Python object of type " << cpp_type_name << " is not None and is not a " + << expected_type_name << " object"; + return Status::TypeError(ss.str()); +} + Status PandasConverter::ConvertDates(std::shared_ptr* out) { PyAcquireGIL lock; @@ -427,8 +458,10 @@ Status PandasConverter::ConvertDates(std::shared_ptr* out) { if (PyDate_CheckExact(obj)) { PyDateTime_Date* pydate = reinterpret_cast(obj); date_builder.Append(PyDate_to_ms(pydate)); - } else { + } else if (PyObject_is_null(obj)) { date_builder.AppendNull(); + } else { + return InvalidConversion(obj, "date"); } } return date_builder.Finish(out); @@ -483,14 +516,18 @@ Status PandasConverter::ConvertBooleans(std::shared_ptr* out) { memset(bitmap, 0, nbytes); int64_t null_count = 0; + PyObject* obj; for (int64_t i = 0; i < length_; ++i) { - if (objects[i] == Py_True) { + obj = objects[i]; + if (obj == Py_True) { BitUtil::SetBit(bitmap, i); BitUtil::SetBit(null_bitmap_data_, i); - } else if (objects[i] != Py_False) { + } else if (obj == Py_False) { + BitUtil::SetBit(null_bitmap_data_, i); + } else if (PyObject_is_null(obj)) { ++null_count; } else { - BitUtil::SetBit(null_bitmap_data_, i); + return InvalidConversion(obj, "bool"); } } @@ -551,7 +588,8 @@ Status PandasConverter::ConvertObjects(std::shared_ptr* out) { } else if (PyDate_CheckExact(objects[i])) { return ConvertDates(out); } else { - return Status::TypeError("unhandled python type"); + return InvalidConversion( + const_cast(objects[i]), "string, bool, or date"); } } } diff --git a/cpp/src/arrow/python/pandas_convert.h b/cpp/src/arrow/python/pandas_convert.h index 12644d98da15..105c1598d393 100644 --- a/cpp/src/arrow/python/pandas_convert.h +++ b/cpp/src/arrow/python/pandas_convert.h @@ -24,6 +24,7 @@ #include #include +#include #include "arrow/util/visibility.h" @@ -73,6 +74,9 @@ ARROW_EXPORT Status PandasObjectsToArrow(MemoryPool* pool, PyObject* ao, PyObject* mo, const std::shared_ptr& type, std::shared_ptr* out); +ARROW_EXPORT +Status InvalidConversion(PyObject* obj, const std::string& expected_type_name); + } // namespace py } // namespace arrow diff --git a/cpp/src/arrow/python/pandas-test.cc b/cpp/src/arrow/python/python-test.cc similarity index 70% rename from cpp/src/arrow/python/pandas-test.cc rename to cpp/src/arrow/python/python-test.cc index a4e640b83718..01e30f5a36ce 100644 --- a/cpp/src/arrow/python/pandas-test.cc +++ b/cpp/src/arrow/python/python-test.cc @@ -17,19 +17,18 @@ #include "gtest/gtest.h" -#include #include -#include -#include + +#include #include "arrow/array.h" #include "arrow/builder.h" #include "arrow/table.h" #include "arrow/test-util.h" -#include "arrow/type.h" #include "arrow/python/common.h" #include "arrow/python/pandas_convert.h" +#include "arrow/python/builtin_convert.h" namespace arrow { namespace py { @@ -65,5 +64,33 @@ TEST(PandasConversionTest, TestObjectBlockWriteFails) { Py_END_ALLOW_THREADS; } +TEST(BuiltinConversionTest, TestMixedTypeFails) { + PyAcquireGIL lock; + MemoryPool* pool = default_memory_pool(); + std::shared_ptr arr; + + OwnedRef list_ref(PyList_New(3)); + PyObject* list = list_ref.obj(); + + ASSERT_NE(list, nullptr); + + PyObject* str = PyUnicode_FromString("abc"); + ASSERT_NE(str, nullptr); + + PyObject* integer = PyLong_FromLong(1234L); + ASSERT_NE(integer, nullptr); + + PyObject* doub = PyFloat_FromDouble(123.0234); + ASSERT_NE(doub, nullptr); + + // This steals a reference to each object, so we don't need to decref them later + // just the list + ASSERT_EQ(PyList_SetItem(list, 0, str), 0); + ASSERT_EQ(PyList_SetItem(list, 1, integer), 0); + ASSERT_EQ(PyList_SetItem(list, 2, doub), 0); + + ASSERT_RAISES(UnknownError, ConvertPySequence(list, pool, &arr)); +} + } // namespace py } // namespace arrow diff --git a/python/pyarrow/tests/test_convert_builtin.py b/python/pyarrow/tests/test_convert_builtin.py index 99251250499d..3309ba018628 100644 --- a/python/pyarrow/tests/test_convert_builtin.py +++ b/python/pyarrow/tests/test_convert_builtin.py @@ -157,3 +157,8 @@ def test_list_of_int(self): assert arr.null_count == 1 assert arr.type == pyarrow.list_(pyarrow.int64()) assert arr.to_pylist() == data + + def test_mixed_types_fails(self): + data = ['a', 1, 2.0] + with self.assertRaises(pyarrow.error.ArrowException): + pyarrow.from_pylist(data) diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py index f7cb47f68559..3f19b68fe0a0 100644 --- a/python/pyarrow/tests/test_convert_pandas.py +++ b/python/pyarrow/tests/test_convert_pandas.py @@ -398,3 +398,8 @@ def test_category(self): ] for values in arrays: self._check_array_roundtrip(values) + + def test_mixed_types_fails(self): + data = pd.DataFrame({'a': ['a', 1, 2.0]}) + with self.assertRaises(A.error.ArrowException): + A.Table.from_pandas(data)