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 2016/03/08 21:48:10 UTC
arrow git commit: ARROW-43: Python: format array values to in
__repr__ for interactive computing
Repository: arrow
Updated Branches:
refs/heads/master ae95dbd18 -> 45cd9fd8d
ARROW-43: Python: format array values to in __repr__ for interactive computing
Author: Wes McKinney <we...@apache.org>
Closes #21 from wesm/ARROW-43 and squashes the following commits:
dee6ba2 [Wes McKinney] Basic array formatter, not tweaking too much for now
Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/45cd9fd8
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/45cd9fd8
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/45cd9fd8
Branch: refs/heads/master
Commit: 45cd9fd8ddc75f5c8a558024c705ab8d37bbc5b5
Parents: ae95dbd
Author: Wes McKinney <we...@apache.org>
Authored: Tue Mar 8 12:48:42 2016 -0800
Committer: Wes McKinney <we...@apache.org>
Committed: Tue Mar 8 12:48:42 2016 -0800
----------------------------------------------------------------------
python/arrow/array.pxd | 1 +
python/arrow/array.pyx | 16 ++++++++++++++-
python/arrow/scalar.pxd | 2 +-
python/arrow/scalar.pyx | 11 ++++++++---
python/arrow/tests/test_array.py | 37 +++++++++++++++++++++++++++++++++++
5 files changed, 62 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/arrow/blob/45cd9fd8/python/arrow/array.pxd
----------------------------------------------------------------------
diff --git a/python/arrow/array.pxd b/python/arrow/array.pxd
index 04dd8d1..482f8f7 100644
--- a/python/arrow/array.pxd
+++ b/python/arrow/array.pxd
@@ -34,6 +34,7 @@ cdef class Array:
DataType type
cdef init(self, const shared_ptr[CArray]& sp_array)
+ cdef getitem(self, int i)
cdef class BooleanArray(Array):
http://git-wip-us.apache.org/repos/asf/arrow/blob/45cd9fd8/python/arrow/array.pyx
----------------------------------------------------------------------
diff --git a/python/arrow/array.pyx b/python/arrow/array.pyx
index 8ebd01d..b367e3b 100644
--- a/python/arrow/array.pyx
+++ b/python/arrow/array.pyx
@@ -46,6 +46,17 @@ cdef class Array:
def __get__(self):
return self.sp_array.get().null_count()
+ def __iter__(self):
+ for i in range(len(self)):
+ yield self.getitem(i)
+ raise StopIteration
+
+ def __repr__(self):
+ from arrow.formatting import array_format
+ type_format = object.__repr__(self)
+ values = array_format(self, window=10)
+ return '{0}\n{1}'.format(type_format, values)
+
def __len__(self):
return self.sp_array.get().length()
@@ -74,7 +85,10 @@ cdef class Array:
while key < 0:
key += len(self)
- return scalar.box_arrow_scalar(self.type, self.sp_array, key)
+ return self.getitem(key)
+
+ cdef getitem(self, int i):
+ return scalar.box_arrow_scalar(self.type, self.sp_array, i)
def slice(self, start, end):
pass
http://git-wip-us.apache.org/repos/asf/arrow/blob/45cd9fd8/python/arrow/scalar.pxd
----------------------------------------------------------------------
diff --git a/python/arrow/scalar.pxd b/python/arrow/scalar.pxd
index 15cdc95..4e0a364 100644
--- a/python/arrow/scalar.pxd
+++ b/python/arrow/scalar.pxd
@@ -55,7 +55,7 @@ cdef class ListValue(ArrayValue):
cdef:
CListArray* ap
- cdef _getitem(self, int i)
+ cdef getitem(self, int i)
cdef class StringValue(ArrayValue):
http://git-wip-us.apache.org/repos/asf/arrow/blob/45cd9fd8/python/arrow/scalar.pyx
----------------------------------------------------------------------
diff --git a/python/arrow/scalar.pyx b/python/arrow/scalar.pyx
index 951ede2..72a280e 100644
--- a/python/arrow/scalar.pyx
+++ b/python/arrow/scalar.pyx
@@ -144,14 +144,19 @@ cdef class ListValue(ArrayValue):
return self.ap.value_length(self.index)
def __getitem__(self, i):
- return self._getitem(i)
+ return self.getitem(i)
+
+ def __iter__(self):
+ for i in range(len(self)):
+ yield self.getitem(i)
+ raise StopIteration
cdef void _set_array(self, const shared_ptr[CArray]& sp_array):
self.sp_array = sp_array
self.ap = <CListArray*> sp_array.get()
self.value_type = box_data_type(self.ap.value_type())
- cdef _getitem(self, int i):
+ cdef getitem(self, int i):
cdef int j = self.ap.offset(self.index) + i
return box_arrow_scalar(self.value_type, self.ap.values(), j)
@@ -161,7 +166,7 @@ cdef class ListValue(ArrayValue):
list result = []
for j in range(len(self)):
- result.append(self._getitem(j).as_py())
+ result.append(self.getitem(j).as_py())
return result
http://git-wip-us.apache.org/repos/asf/arrow/blob/45cd9fd8/python/arrow/tests/test_array.py
----------------------------------------------------------------------
diff --git a/python/arrow/tests/test_array.py b/python/arrow/tests/test_array.py
index 8eaa533..ebd872c 100644
--- a/python/arrow/tests/test_array.py
+++ b/python/arrow/tests/test_array.py
@@ -17,6 +17,7 @@
from arrow.compat import unittest
import arrow
+import arrow.formatting as fmt
class TestArrayAPI(unittest.TestCase):
@@ -24,3 +25,39 @@ class TestArrayAPI(unittest.TestCase):
def test_getitem_NA(self):
arr = arrow.from_pylist([1, None, 2])
assert arr[1] is arrow.NA
+
+ def test_list_format(self):
+ arr = arrow.from_pylist([[1], None, [2, 3]])
+ result = fmt.array_format(arr)
+ expected = """\
+[
+ [1],
+ NA,
+ [2,
+ 3]
+]"""
+ assert result == expected
+
+ def test_string_format(self):
+ arr = arrow.from_pylist(['foo', None, 'bar'])
+ result = fmt.array_format(arr)
+ expected = """\
+[
+ 'foo',
+ NA,
+ 'bar'
+]"""
+ assert result == expected
+
+ def test_long_array_format(self):
+ arr = arrow.from_pylist(range(100))
+ result = fmt.array_format(arr, window=2)
+ expected = """\
+[
+ 0,
+ 1,
+ ...
+ 98,
+ 99
+]"""
+ assert result == expected