emkornfield commented on a change in pull request #11302: URL: https://github.com/apache/arrow/pull/11302#discussion_r721869652
########## File path: cpp/src/arrow/python/arrow_to_python.h ########## @@ -0,0 +1,161 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Functions for converting between pandas's NumPy-based data representation +// and Arrow data structures + +#pragma once + +#include "arrow/chunked_array.h" +#include "arrow/python/common.h" +#include "arrow/python/platform.h" +#include "arrow/util/hashing.h" + +namespace arrow { + +class Array; +class Scalar; + +namespace py { + +struct ArrowToPythonObjectOptions { + MemoryPool* pool = default_memory_pool(); + bool deduplicate_objects = false; +}; + +class ARROW_PYTHON_EXPORT ArrowToPython { + public: + // \brief Converts the given Array to a PyList object. Returns NULL if there + // is an error converting the Array. The list elements are the same ones + // generated via ToLogical() + // + // N.B. This has limited type support. ARROW-12976 tracks extending the implementation. + Result<PyObject*> ToPyList(const Array& array); + + // Populates out_objects with the result of converting the array values + // to python objects. The same logic as ToLogical(). + // + // N.B. Not all types are supported. ARROW-12976 tracks extending the implementation. + Status ToNumpyObjectArray(const ArrowToPythonObjectOptions& options, + const ChunkedArray& array, PyObject** out_objects); + + // \brief Converts the given Scalar to a python object that best corresponds + // with the Scalar's type. + // + // For example timestamp[ms] is translated into datetime.datetime. + // + // N.B. This has limited type support. ARROW-12976 tracks extending the implementation. + Result<PyObject*> ToLogical(const Scalar& scalar); + + // \brief Converts the given Scalar the type that is closed to its arrow + // representation. + // + // For instance timestamp would be translated to a integer representing an + // offset from the unix epoch. + // + // Returns nullptr on error. + // + // GIL must be health when calling this method. + // N.B. This has limited type support. ARROW-12976 tracks full implementation. + Result<PyObject*> ToPrimitive(const Scalar& scalar); + + private: + Status Init(); +}; + +namespace internal { +// TODO(ARROW-12976): See if we can refactor Pandas ObjectWriter logic +// to the .cc file and move this there as well if we can. + +// Generic Array -> PyObject** converter that handles object deduplication, if +// requested +template <typename ArrayType, typename WriteValue, typename Assigner> +inline Status WriteArrayObjects(const ArrayType& arr, WriteValue&& write_func, + Assigner out_values) { + // TODO(ARROW-12976): Use visitor here? + const bool has_nulls = arr.null_count() > 0; + for (int64_t i = 0; i < arr.length(); ++i) { + if (has_nulls && arr.IsNull(i)) { + Py_INCREF(Py_None); + *out_values = Py_None; + } else { + RETURN_NOT_OK(write_func(arr.GetView(i), out_values)); + } + ++out_values; + } Review comment: this was moved from existing code base, I don't know but would prefer to handle the TODO above use Visitor in general. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
