You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ap...@apache.org on 2018/05/10 15:29:12 UTC
[arrow] branch master updated: ARROW-1964: [Python] Expose
StringBuilder to Python
This is an automated email from the ASF dual-hosted git repository.
apitrou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 414268b ARROW-1964: [Python] Expose StringBuilder to Python
414268b is described below
commit 414268ba8e303e77fb41df3872bbc58ce8852ef4
Author: Donal Simmie <do...@gmail.com>
AuthorDate: Thu May 10 17:28:53 2018 +0200
ARROW-1964: [Python] Expose StringBuilder to Python
* Partial implementation of ARROW-1964
* Only implements StringBuilder not the other builders, notably the DictionaryBuilder mentioned in issue 1964
Author: Donal Simmie <do...@gmail.com>
Closes #1930 from dsimmie/ARROW-1964-Partial and squashes the following commits:
e4e54ec <Donal Simmie> Fixed review comments from #1930
be2bd39 <Donal Simmie> Exposed StringBuilder to Cython - partial implementation of ARROW-1964
---
python/pyarrow/builder.pxi | 83 ++++++++++++++++++++++++++++++++++++
python/pyarrow/includes/libarrow.pxd | 19 +++++++++
python/pyarrow/lib.pyx | 3 ++
python/pyarrow/tests/test_builder.py | 57 +++++++++++++++++++++++++
4 files changed, 162 insertions(+)
diff --git a/python/pyarrow/builder.pxi b/python/pyarrow/builder.pxi
new file mode 100644
index 0000000..faeed6c
--- /dev/null
+++ b/python/pyarrow/builder.pxi
@@ -0,0 +1,83 @@
+# 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.
+
+import six
+from pyarrow.compat import tobytes
+
+cdef class StringBuilder:
+ """
+ Builder class for UTF8 strings. This class exposes facilities for
+ incrementally adding string values and building the null bitmap
+ for a pyarrow.Array (type='string').
+ """
+ cdef:
+ unique_ptr[CStringBuilder] builder
+
+ def __cinit__(self, MemoryPool memory_pool=None):
+ cdef CMemoryPool* pool = maybe_unbox_memory_pool(memory_pool)
+ self.builder.reset(new CStringBuilder(pool))
+
+ def append(self, value):
+ """
+ Append a single value to the builder. The value can either be a
+ string/bytes object or a null value (np.nan or None).
+
+ Parameters
+ ----------
+ value : string/bytes or np.nan/None
+ The value to append to the string array builder
+ """
+ if value is None or value is np.nan:
+ self.builder.get().AppendNull()
+ elif isinstance(value, (six.string_types, six.binary_type)):
+ self.builder.get().Append(tobytes(value))
+ else:
+ raise TypeError('StringBuilder only accepts string objects')
+
+ def append_values(self, values):
+ """
+ Append all the values in an iterable to the string array builder
+ object.
+
+ Parameters
+ ----------
+ values : iterable of string/bytes or np.nan/None values
+ The values to append to the string array builder
+ """
+ for value in values:
+ self.append(value)
+
+ def finish(self):
+ """
+ Return result of builder as an Array object; also resets the builder.
+
+ Returns
+ -------
+ array : pyarrow.Array
+ """
+ cdef shared_ptr[CArray] out
+ with nogil:
+ self.builder.get().Finish(&out)
+ return pyarrow_wrap_array(out)
+
+ property null_count:
+
+ def __get__(self):
+ return self.builder.get().null_count()
+
+ def __len__(self):
+ return self.builder.get().length()
diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd
index 215b102..9a0b468 100644
--- a/python/pyarrow/includes/libarrow.pxd
+++ b/python/pyarrow/includes/libarrow.pxd
@@ -19,6 +19,7 @@
from pyarrow.includes.common cimport *
+
cdef extern from "arrow/util/key_value_metadata.h" namespace "arrow" nogil:
cdef cppclass CKeyValueMetadata" arrow::KeyValueMetadata":
CKeyValueMetadata()
@@ -533,6 +534,24 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
CStatus ConcatenateTables(const vector[shared_ptr[CTable]]& tables,
shared_ptr[CTable]* result)
+ cdef extern from "arrow/builder.h" namespace "arrow" nogil:
+
+ cdef cppclass CArrayBuilder" arrow::ArrayBuilder":
+ CArrayBuilder(shared_ptr[CDataType], CMemoryPool* pool)
+
+ cdef cppclass CBinaryBuilder" arrow::BinaryBuilder"(CArrayBuilder):
+ CArrayBuilder(shared_ptr[CDataType], CMemoryPool* pool)
+
+ cdef cppclass CStringBuilder" arrow::StringBuilder"(CBinaryBuilder):
+ CStringBuilder(CMemoryPool* pool)
+
+ int64_t length()
+ int64_t null_count()
+
+ CStatus Append(const c_string& value)
+ CStatus AppendNull()
+ CStatus Finish(shared_ptr[CArray]* out)
+
cdef extern from "arrow/io/api.h" namespace "arrow::io" nogil:
enum FileMode" arrow::io::FileMode::type":
diff --git a/python/pyarrow/lib.pyx b/python/pyarrow/lib.pyx
index 8929ea0..4c22fc4 100644
--- a/python/pyarrow/lib.pyx
+++ b/python/pyarrow/lib.pyx
@@ -106,6 +106,9 @@ include "scalar.pxi"
# Array types
include "array.pxi"
+# Builders
+include "builder.pxi"
+
# Column, Table, Record Batch
include "table.pxi"
diff --git a/python/pyarrow/tests/test_builder.py b/python/pyarrow/tests/test_builder.py
new file mode 100644
index 0000000..c11c970
--- /dev/null
+++ b/python/pyarrow/tests/test_builder.py
@@ -0,0 +1,57 @@
+# 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.
+
+import numpy as np
+
+import pyarrow as pa
+from pyarrow.lib import StringBuilder
+
+
+def test_string_builder_append():
+ sbuilder = StringBuilder()
+ sbuilder.append(b"a byte string")
+ sbuilder.append("a string")
+ sbuilder.append(np.nan)
+ sbuilder.append(None)
+ assert len(sbuilder) == 4
+ assert sbuilder.null_count == 2
+ arr = sbuilder.finish()
+ assert len(sbuilder) == 0
+ assert isinstance(arr, pa.Array)
+ assert arr.null_count == 2
+ assert arr.type == 'str'
+ expected = ["a byte string", "a string", None, None]
+ assert arr.to_pylist() == expected
+
+
+def test_string_builder_append_values():
+ sbuilder = StringBuilder()
+ sbuilder.append_values([np.nan, None, "text", None, "other text"])
+ assert sbuilder.null_count == 3
+ arr = sbuilder.finish()
+ assert arr.null_count == 3
+ expected = [None, None, "text", None, "other text"]
+ assert arr.to_pylist() == expected
+
+
+def test_string_builder_append_after_finish():
+ sbuilder = StringBuilder()
+ sbuilder.append_values([np.nan, None, "text", None, "other text"])
+ arr = sbuilder.finish()
+ sbuilder.append("No effect")
+ expected = [None, None, "text", None, "other text"]
+ assert arr.to_pylist() == expected
--
To stop receiving notification emails like this one, please contact
apitrou@apache.org.