You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2017/07/04 15:32:25 UTC

arrow git commit: ARROW-966: [Python] Also accept Field instance in pyarrow.list_

Repository: arrow
Updated Branches:
  refs/heads/master e5a08dd8f -> 7c18ddd4e


ARROW-966: [Python] Also accept Field instance in pyarrow.list_

Author: Wes McKinney <we...@twosigma.com>

Closes #809 from wesm/ARROW-966 and squashes the following commits:

a20f54dd [Wes McKinney] Also accept Field instance in pyarrow.list_


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/7c18ddd4
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/7c18ddd4
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/7c18ddd4

Branch: refs/heads/master
Commit: 7c18ddd4e6bc4b29cd4dd23786760f5c00137bf9
Parents: e5a08dd
Author: Wes McKinney <we...@twosigma.com>
Authored: Tue Jul 4 11:32:20 2017 -0400
Committer: Wes McKinney <we...@twosigma.com>
Committed: Tue Jul 4 11:32:20 2017 -0400

----------------------------------------------------------------------
 python/pyarrow/array.pxi             | 60 ++++++++++++++++++++++++++-----
 python/pyarrow/includes/libarrow.pxd |  1 +
 python/pyarrow/tests/test_schema.py  |  4 +++
 3 files changed, 57 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/7c18ddd4/python/pyarrow/array.pxi
----------------------------------------------------------------------
diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index 79e88fc..6a49256 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -622,17 +622,46 @@ def binary(int length=-1):
     return pyarrow_wrap_data_type(fixed_size_binary_type)
 
 
-def list_(DataType value_type):
-    cdef DataType out = DataType()
+def list_(value_type):
+    """
+    Create ListType instance from child data type or field
+
+    Parameters
+    ----------
+    value_type : DataType or Field
+
+    Returns
+    -------
+    list_type : DataType
+    """
+    cdef:
+        DataType data_type
+        Field field
+
     cdef shared_ptr[CDataType] list_type
-    list_type.reset(new CListType(value_type.sp_type))
-    out.init(list_type)
-    return out
+
+    if isinstance(value_type, DataType):
+        list_type.reset(new CListType((<DataType> value_type).sp_type))
+    elif isinstance(value_type, Field):
+        list_type.reset(new CListType((<Field> value_type).sp_field))
+    else:
+        raise ValueError('List requires DataType or Field')
+
+    return pyarrow_wrap_data_type(list_type)
 
 
 def dictionary(DataType index_type, Array dictionary):
     """
     Dictionary (categorical, or simply encoded) type
+
+    Parameters
+    ----------
+    index_type : DataType
+    dictionary : Array
+
+    Returns
+    -------
+    type : DictionaryType
     """
     cdef DictionaryType out = DictionaryType()
     cdef shared_ptr[CDataType] dict_type
@@ -644,10 +673,26 @@ def dictionary(DataType index_type, Array dictionary):
 
 def struct(fields):
     """
+    Create StructType instance from fields
 
+    Parameters
+    ----------
+    fields : sequence of Field values
+
+    Examples
+    --------
+    import pyarrow as pa
+    fields = [
+        pa.field('f1', pa.int32()),
+        pa.field('f2', pa.string())
+    ]
+    struct_type = pa.struct(fields)
+
+    Returns
+    -------
+    type : DataType
     """
     cdef:
-        DataType out = DataType()
         Field field
         vector[shared_ptr[CField]] c_fields
         cdef shared_ptr[CDataType] struct_type
@@ -656,8 +701,7 @@ def struct(fields):
         c_fields.push_back(field.sp_field)
 
     struct_type.reset(new CStructType(c_fields))
-    out.init(struct_type)
-    return out
+    return pyarrow_wrap_data_type(struct_type)
 
 
 def schema(fields):

http://git-wip-us.apache.org/repos/asf/arrow/blob/7c18ddd4/python/pyarrow/includes/libarrow.pxd
----------------------------------------------------------------------
diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd
index fb101fe..902f98e 100644
--- a/python/pyarrow/includes/libarrow.pxd
+++ b/python/pyarrow/includes/libarrow.pxd
@@ -160,6 +160,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
 
     cdef cppclass CListType" arrow::ListType"(CDataType):
         CListType(const shared_ptr[CDataType]& value_type)
+        CListType(const shared_ptr[CField]& field)
 
     cdef cppclass CStringType" arrow::StringType"(CDataType):
         pass

http://git-wip-us.apache.org/repos/asf/arrow/blob/7c18ddd4/python/pyarrow/tests/test_schema.py
----------------------------------------------------------------------
diff --git a/python/pyarrow/tests/test_schema.py b/python/pyarrow/tests/test_schema.py
index a6fe1a5..f920e8d 100644
--- a/python/pyarrow/tests/test_schema.py
+++ b/python/pyarrow/tests/test_schema.py
@@ -64,6 +64,10 @@ def test_type_list():
     list_type = pa.list_(value_type)
     assert str(list_type) == 'list<item: int32>'
 
+    field = pa.field('my_item', pa.string())
+    l2 = pa.list_(field)
+    assert str(l2) == 'list<my_item: string>'
+
 
 def test_type_string():
     t = pa.string()