This is an automated email from the ASF dual-hosted git repository.

wesm 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 90d9daf  ARROW-2129: [Python] Handle conversion of empty tables to 
Pandas
90d9daf is described below

commit 90d9daf1419fdff5fd295053dcad756d1cdb3931
Author: Uwe L. Korn <uw...@xhochy.com>
AuthorDate: Mon Feb 12 09:55:09 2018 -0500

    ARROW-2129: [Python] Handle conversion of empty tables to Pandas
    
    Opened https://issues.apache.org/jira/browse/ARROW-2133 to handle the 
nested case.
    
    Author: Uwe L. Korn <uw...@xhochy.com>
    
    Closes #1588 from xhochy/ARROW-2129 and squashes the following commits:
    
    78b62d3f [Uwe L. Korn] Use Pandas' testing facilities
    8287d034 [Uwe L. Korn] ARROW-2129: [Python] Handle conversion of empty 
tables to Pandas
---
 cpp/src/arrow/python/arrow_to_pandas.cc     | 11 ++++++++---
 python/pyarrow/tests/test_convert_pandas.py |  6 ++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/cpp/src/arrow/python/arrow_to_pandas.cc 
b/cpp/src/arrow/python/arrow_to_pandas.cc
index 60a2eae..0488989 100644
--- a/cpp/src/arrow/python/arrow_to_pandas.cc
+++ b/cpp/src/arrow/python/arrow_to_pandas.cc
@@ -280,6 +280,9 @@ class PandasBlock {
 
 template <typename T>
 inline const T* GetPrimitiveValues(const Array& arr) {
+  if (arr.length() == 0) {
+    return nullptr;
+  }
   const auto& prim_arr = static_cast<const PrimitiveArray&>(arr);
   const T* raw_values = reinterpret_cast<const T*>(prim_arr.values()->data());
   return raw_values + arr.offset();
@@ -304,9 +307,11 @@ inline void ConvertIntegerNoNullsSameType(PandasOptions 
options, const ChunkedAr
                                           T* out_values) {
   for (int c = 0; c < data.num_chunks(); c++) {
     const auto& arr = *data.chunk(c);
-    const T* in_values = GetPrimitiveValues<T>(arr);
-    memcpy(out_values, in_values, sizeof(T) * arr.length());
-    out_values += arr.length();
+    if (arr.length() > 0) {
+      const T* in_values = GetPrimitiveValues<T>(arr);
+      memcpy(out_values, in_values, sizeof(T) * arr.length());
+      out_values += arr.length();
+    }
   }
 }
 
diff --git a/python/pyarrow/tests/test_convert_pandas.py 
b/python/pyarrow/tests/test_convert_pandas.py
index edb6051..b825dfc 100644
--- a/python/pyarrow/tests/test_convert_pandas.py
+++ b/python/pyarrow/tests/test_convert_pandas.py
@@ -1346,6 +1346,12 @@ class TestPandasConversion(object):
         _check_pandas_roundtrip(df2, preserve_index=True)
         _check_pandas_roundtrip(df2, as_batch=True, preserve_index=True)
 
+    def test_convert_empty_table(self):
+        arr = pa.array([], type=pa.int64())
+        tm.assert_almost_equal(arr.to_pandas(), np.array([], dtype=np.int64))
+        arr = pa.array([], type=pa.string())
+        tm.assert_almost_equal(arr.to_pandas(), np.array([], dtype=object))
+
     def test_array_from_pandas_date_with_mask(self):
         m = np.array([True, False, True])
         data = pd.Series([

-- 
To stop receiving notification emails like this one, please contact
w...@apache.org.

Reply via email to