]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/python/pyarrow/tests/pyarrow_cython_example.pyx
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / python / pyarrow / tests / pyarrow_cython_example.pyx
CommitLineData
1d09f67e
TL
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# distutils: language=c++
19# cython: language_level = 3
20
21from pyarrow.lib cimport *
22
23
24def get_array_length(obj):
25 # An example function accessing both the pyarrow Cython API
26 # and the Arrow C++ API
27 cdef shared_ptr[CArray] arr = pyarrow_unwrap_array(obj)
28 if arr.get() == NULL:
29 raise TypeError("not an array")
30 return arr.get().length()
31
32
33def make_null_array(length):
34 # An example function that returns a PyArrow object without PyArrow
35 # being imported explicitly at the Python level.
36 cdef shared_ptr[CArray] null_array
37 null_array.reset(new CNullArray(length))
38 return pyarrow_wrap_array(null_array)
39
40
41def cast_scalar(scalar, to_type):
42 cdef:
43 shared_ptr[CScalar] c_scalar
44 shared_ptr[CDataType] c_type
45 CResult[shared_ptr[CScalar]] c_result
46
47 c_scalar = pyarrow_unwrap_scalar(scalar)
48 if c_scalar.get() == NULL:
49 raise TypeError("not a scalar")
50 c_type = pyarrow_unwrap_data_type(to_type)
51 if c_type.get() == NULL:
52 raise TypeError("not a type")
53 c_result = c_scalar.get().CastTo(c_type)
54 c_scalar = GetResultValue(c_result)
55 return pyarrow_wrap_scalar(c_scalar)