ARROW-439: [Python] Add option in "to_pandas" conversions to yield Categorical from...
[arrow.git] / cpp / src / arrow / python / python-test.cc
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include "gtest/gtest.h"
19
20 #include <memory>
21
22 #include "arrow/python/platform.h"
23
24 #include "arrow/array.h"
25 #include "arrow/builder.h"
26 #include "arrow/table.h"
27 #include "arrow/test-util.h"
28
29 #include "arrow/python/arrow_to_pandas.h"
30 #include "arrow/python/builtin_convert.h"
31 #include "arrow/python/common.h"
32 #include "arrow/python/helpers.h"
33
34 #include "arrow/util/decimal.h"
35
36 namespace arrow {
37 namespace py {
38
39 TEST(PyBuffer, InvalidInputObject) { PyBuffer buffer(Py_None); }
40
41 TEST(DecimalTest, TestPythonDecimalToString) {
42 PyAcquireGIL lock;
43
44 OwnedRef decimal;
45 OwnedRef Decimal;
46 ASSERT_OK(ImportModule("decimal", &decimal));
47 ASSERT_NE(decimal.obj(), nullptr);
48
49 ASSERT_OK(ImportFromModule(decimal, "Decimal", &Decimal));
50 ASSERT_NE(Decimal.obj(), nullptr);
51
52 std::string decimal_string("-39402950693754869342983");
53 const char* format = "s#";
54 auto c_string = decimal_string.c_str();
55 ASSERT_NE(c_string, nullptr);
56
57 auto c_string_size = decimal_string.size();
58 ASSERT_GT(c_string_size, 0);
59 OwnedRef pydecimal(PyObject_CallFunction(Decimal.obj(), const_cast<char*>(format),
60 c_string, c_string_size));
61 ASSERT_NE(pydecimal.obj(), nullptr);
62 ASSERT_EQ(PyErr_Occurred(), nullptr);
63
64 boost::multiprecision::int128_t boost_decimal(decimal_string);
65 PyObject* python_object = pydecimal.obj();
66 ASSERT_NE(python_object, nullptr);
67
68 std::string string_result;
69 ASSERT_OK(PythonDecimalToString(python_object, &string_result));
70 ASSERT_EQ(boost_decimal.str(), string_result);
71 }
72
73 TEST(PandasConversionTest, TestObjectBlockWriteFails) {
74 StringBuilder builder;
75 const char value[] = {'\xf1', '\0'};
76
77 for (int i = 0; i < 1000; ++i) {
78 ASSERT_OK(builder.Append(value, static_cast<int32_t>(strlen(value))));
79 }
80
81 std::shared_ptr<Array> arr;
82 ASSERT_OK(builder.Finish(&arr));
83
84 auto f1 = field("f1", utf8());
85 auto f2 = field("f2", utf8());
86 auto f3 = field("f3", utf8());
87 std::vector<std::shared_ptr<Field>> fields = {f1, f2, f3};
88 std::vector<std::shared_ptr<Array>> cols = {arr, arr, arr};
89
90 auto schema = std::make_shared<Schema>(fields);
91 auto table = std::make_shared<Table>(schema, cols);
92
93 PyObject* out;
94 Py_BEGIN_ALLOW_THREADS;
95 PandasOptions options;
96 MemoryPool* pool = default_memory_pool();
97 ASSERT_RAISES(UnknownError, ConvertTableToPandas(options, table, 2, pool, &out));
98 Py_END_ALLOW_THREADS;
99 }
100
101 TEST(BuiltinConversionTest, TestMixedTypeFails) {
102 PyAcquireGIL lock;
103 MemoryPool* pool = default_memory_pool();
104 std::shared_ptr<Array> arr;
105
106 OwnedRef list_ref(PyList_New(3));
107 PyObject* list = list_ref.obj();
108
109 ASSERT_NE(list, nullptr);
110
111 PyObject* str = PyUnicode_FromString("abc");
112 ASSERT_NE(str, nullptr);
113
114 PyObject* integer = PyLong_FromLong(1234L);
115 ASSERT_NE(integer, nullptr);
116
117 PyObject* doub = PyFloat_FromDouble(123.0234);
118 ASSERT_NE(doub, nullptr);
119
120 // This steals a reference to each object, so we don't need to decref them later
121 // just the list
122 ASSERT_EQ(PyList_SetItem(list, 0, str), 0);
123 ASSERT_EQ(PyList_SetItem(list, 1, integer), 0);
124 ASSERT_EQ(PyList_SetItem(list, 2, doub), 0);
125
126 ASSERT_RAISES(UnknownError, ConvertPySequence(list, pool, &arr));
127 }
128
129 } // namespace py
130 } // namespace arrow