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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
19 cdef class StringBuilder(_Weakrefable):
21 Builder class for UTF8 strings.
23 This class exposes facilities for incrementally adding string values and
24 building the null bitmap for a pyarrow.Array (type='string').
27 unique_ptr[CStringBuilder] builder
29 def __cinit__(self, MemoryPool memory_pool=None):
30 cdef CMemoryPool* pool = maybe_unbox_memory_pool(memory_pool)
31 self.builder.reset(new CStringBuilder(pool))
33 def append(self, value):
35 Append a single value to the builder.
37 The value can either be a string/bytes object or a null value
42 value : string/bytes or np.nan/None
43 The value to append to the string array builder.
45 if value is None or value is np.nan:
46 self.builder.get().AppendNull()
47 elif isinstance(value, (bytes, str)):
48 self.builder.get().Append(tobytes(value))
50 raise TypeError('StringBuilder only accepts string objects')
52 def append_values(self, values):
54 Append all the values from an iterable.
58 values : iterable of string/bytes or np.nan/None values
59 The values to append to the string array builder.
66 Return result of builder as an Array object; also resets the builder.
72 cdef shared_ptr[CArray] out
74 self.builder.get().Finish(&out)
75 return pyarrow_wrap_array(out)
79 return self.builder.get().null_count()
82 return self.builder.get().length()