mirror of
https://github.com/apache/arrow.git
synced 2026-10-29 00:56:31 +08:00
Expose arrow::compute::Cast in Python as Array.cast. Still need to write tests
Change-Id: Ibb15d121a404c89a220b5f8f9cee8ff9db833b41
This commit is contained in:
1 parent
a9a04c9cac
commit
c1b4570961
8 files changed
+134
-6
No files matched your search
@@ -17,6 +17,7 @@
|
||||
|
||||
# Headers: top level
|
||||
install(FILES
|
||||
api.h
|
||||
cast.h
|
||||
context.h
|
||||
kernel.h
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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.
|
||||
|
||||
#ifndef ARROW_COMPUTE_API_H
|
||||
#define ARROW_COMPUTE_API_H
|
||||
|
||||
#include "arrow/compute/cast.h"
|
||||
#include "arrow/compute/context.h"
|
||||
#include "arrow/compute/kernel.h"
|
||||
|
||||
#endif // ARROW_COMPUTE_API_H
|
||||
@@ -23,8 +23,6 @@
|
||||
#include "arrow/array.h"
|
||||
#include "arrow/util/visibility.h"
|
||||
|
||||
#include "arrow/compute/kernel.h"
|
||||
|
||||
namespace arrow {
|
||||
|
||||
using internal::ArrayData;
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
#include "arrow/compute/cast.h"
|
||||
#include "arrow/compute/context.h"
|
||||
#include "arrow/compute/kernel.h"
|
||||
|
||||
using std::vector;
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#ifndef ARROW_COMPUTE_CONTEXT_H
|
||||
#define ARROW_COMPUTE_CONTEXT_H
|
||||
|
||||
#include "arrow/memory_pool.h"
|
||||
#include "arrow/status.h"
|
||||
#include "arrow/type_fwd.h"
|
||||
#include "arrow/util/visibility.h"
|
||||
@@ -35,7 +36,7 @@ namespace compute {
|
||||
/// \brief Container for variables and options used by function evaluation
|
||||
class ARROW_EXPORT FunctionContext {
|
||||
public:
|
||||
explicit FunctionContext(MemoryPool* pool);
|
||||
explicit FunctionContext(MemoryPool* pool ARROW_MEMORY_POOL_DEFAULT);
|
||||
MemoryPool* memory_pool() const;
|
||||
|
||||
/// \brief Allocate buffer from the context's memory pool
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// 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.
|
||||
|
||||
#ifndef ARROW_COMPUTE_KERNEL_H
|
||||
#define ARROW_COMPUTE_KERNEL_H
|
||||
|
||||
#include "arrow/util/visibility.h"
|
||||
|
||||
namespace arrow {
|
||||
|
||||
class Array;
|
||||
using internal::ArrayData;
|
||||
|
||||
namespace compute {
|
||||
|
||||
class FunctionContext;
|
||||
|
||||
/// \class OpKernel
|
||||
/// \brief Base class for operator kernels
|
||||
class ARROW_EXPORT OpKernel {
|
||||
public:
|
||||
virtual ~OpKernel() = default;
|
||||
};
|
||||
|
||||
/// \class UnaryKernel
|
||||
/// \brief An array-valued function of a single input argument
|
||||
class ARROW_EXPORT UnaryKernel : public OpKernel {
|
||||
public:
|
||||
virtual Status Call(FunctionContext* ctx, const Array& input, ArrayData* out) = 0;
|
||||
};
|
||||
|
||||
} // namespace compute
|
||||
} // namespace arrow
|
||||
|
||||
#endif // ARROW_COMPUTE_KERNEL_H
|
||||
@@ -88,6 +88,19 @@ def _normalize_slice(object arrow_obj, slice key):
|
||||
return arrow_obj.slice(start, stop - start)
|
||||
|
||||
|
||||
cdef class _FunctionContext:
|
||||
cdef:
|
||||
unique_ptr[CFunctionContext] ctx
|
||||
|
||||
def __cinit__(self):
|
||||
self.ctx.reset(new CFunctionContext(c_default_memory_pool()))
|
||||
|
||||
cdef _FunctionContext _global_ctx = _FunctionContext()
|
||||
|
||||
cdef CFunctionContext* _context() nogil:
|
||||
return _global_ctx.ctx.get()
|
||||
|
||||
|
||||
cdef class Array:
|
||||
|
||||
cdef void init(self, const shared_ptr[CArray]& sp_array):
|
||||
@@ -99,6 +112,34 @@ cdef class Array:
|
||||
with nogil:
|
||||
check_status(DebugPrint(deref(self.ap), 0))
|
||||
|
||||
def cast(self, DataType target_type, safe=True):
|
||||
"""
|
||||
Cast array values to another data type
|
||||
|
||||
Parameters
|
||||
----------
|
||||
target_type : DataType
|
||||
Type to cast to
|
||||
safe : boolean, default True
|
||||
Check for overflows or other unsafe conversions
|
||||
|
||||
Returns
|
||||
-------
|
||||
casted : Array
|
||||
"""
|
||||
cdef:
|
||||
CCastOptions options
|
||||
shared_ptr[CArray] result
|
||||
|
||||
if safe:
|
||||
options.allow_int_overflow = 0
|
||||
|
||||
with nogil:
|
||||
check_status(Cast(_context(), self.ap[0], target_type.sp_type,
|
||||
options, &result))
|
||||
|
||||
return pyarrow_wrap_array(result)
|
||||
|
||||
@staticmethod
|
||||
def from_pandas(obj, mask=None, DataType type=None,
|
||||
timestamps_to_ms=False,
|
||||
|
||||
@@ -706,9 +706,6 @@ cdef extern from "arrow/ipc/api.h" namespace "arrow::ipc" nogil:
|
||||
InputStream* stream,
|
||||
shared_ptr[CRecordBatch]* out)
|
||||
|
||||
|
||||
cdef extern from "arrow/ipc/feather.h" namespace "arrow::ipc::feather" nogil:
|
||||
|
||||
cdef cppclass CFeatherWriter" arrow::ipc::feather::TableWriter":
|
||||
@staticmethod
|
||||
CStatus Open(const shared_ptr[OutputStream]& stream,
|
||||
@@ -737,6 +734,21 @@ cdef extern from "arrow/ipc/feather.h" namespace "arrow::ipc::feather" nogil:
|
||||
c_string GetColumnName(int i)
|
||||
|
||||
|
||||
cdef extern from "arrow/compute/api.h" namespace "arrow::compute" nogil:
|
||||
|
||||
cdef cppclass CFunctionContext" arrow::compute::FunctionContext":
|
||||
CFunctionContext()
|
||||
CFunctionContext(CMemoryPool* pool)
|
||||
|
||||
cdef cppclass CCastOptions" arrow::compute::CastOptions":
|
||||
c_bool allow_int_overflow
|
||||
|
||||
CStatus Cast(CFunctionContext* context, const CArray& array,
|
||||
const shared_ptr[CDataType]& to_type,
|
||||
const CCastOptions& options,
|
||||
shared_ptr[CArray]* out)
|
||||
|
||||
|
||||
cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil:
|
||||
shared_ptr[CDataType] GetPrimitiveType(Type type)
|
||||
shared_ptr[CDataType] GetTimestampType(TimeUnit unit)
|
||||
|
||||
Reference in new issue
Block a user