You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ks...@apache.org on 2018/09/30 11:48:58 UTC

[arrow] branch master updated: ARROW-3363: [C++/Python] Add helper functions to detect scalar Python types

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

kszucs 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 7a4b48c  ARROW-3363: [C++/Python] Add helper functions to detect scalar Python types
7a4b48c is described below

commit 7a4b48cbfc0c94760a9753ff5afb0e52b6f6b497
Author: Korn, Uwe <Uw...@blue-yonder.com>
AuthorDate: Sun Sep 30 13:48:47 2018 +0200

    ARROW-3363: [C++/Python] Add helper functions to detect scalar Python types
    
    Author: Korn, Uwe <Uw...@blue-yonder.com>
    Author: Krisztián Szűcs <sz...@gmail.com>
    
    Closes #2659 from xhochy/ARROW-3363 and squashes the following commits:
    
    3560fd35 <Krisztián Szűcs> flake8
    ffcc20c8 <Korn, Uwe> Rename functions to _value
    c7115214 <Korn, Uwe> ARROW-3363:  Add helper functions to detect scalar Python types
---
 cpp/src/arrow/python/inference.cc     |  9 +++++++++
 cpp/src/arrow/python/inference.h      | 12 ++++++++++++
 cpp/src/arrow/python/numpy-internal.h |  4 ++++
 python/pyarrow/includes/libarrow.pxd  |  6 ++++++
 python/pyarrow/tests/test_types.py    | 24 +++++++++++++++++++++++-
 python/pyarrow/types.pxi              | 12 ++++++++++++
 python/pyarrow/types.py               |  4 ++++
 7 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/cpp/src/arrow/python/inference.cc b/cpp/src/arrow/python/inference.cc
index 880fe84..e619a64 100644
--- a/cpp/src/arrow/python/inference.cc
+++ b/cpp/src/arrow/python/inference.cc
@@ -583,5 +583,14 @@ Status InferArrowTypeAndSize(PyObject* obj, int64_t* size,
   return Status::OK();
 }
 
+ARROW_EXPORT
+bool IsPyBool(PyObject* obj) { return internal::PyBoolScalar_Check(obj); }
+
+ARROW_EXPORT
+bool IsPyInt(PyObject* obj) { return internal::PyIntScalar_Check(obj); }
+
+ARROW_EXPORT
+bool IsPyFloat(PyObject* obj) { return internal::PyFloatScalar_Check(obj); }
+
 }  // namespace py
 }  // namespace arrow
diff --git a/cpp/src/arrow/python/inference.h b/cpp/src/arrow/python/inference.h
index 21cddf4..2cffa17 100644
--- a/cpp/src/arrow/python/inference.h
+++ b/cpp/src/arrow/python/inference.h
@@ -48,6 +48,18 @@ ARROW_EXPORT
 arrow::Status InferArrowTypeAndSize(PyObject* obj, int64_t* size,
                                     std::shared_ptr<arrow::DataType>* out_type);
 
+/// Checks whether the passed Python object is a boolean scalar
+ARROW_EXPORT
+bool IsPyBool(PyObject* obj);
+
+/// Checks whether the passed Python object is an integer scalar
+ARROW_EXPORT
+bool IsPyInt(PyObject* obj);
+
+/// Checks whether the passed Python object is a float scalar
+ARROW_EXPORT
+bool IsPyFloat(PyObject* obj);
+
 }  // namespace py
 }  // namespace arrow
 
diff --git a/cpp/src/arrow/python/numpy-internal.h b/cpp/src/arrow/python/numpy-internal.h
index b214a50..463795a 100644
--- a/cpp/src/arrow/python/numpy-internal.h
+++ b/cpp/src/arrow/python/numpy-internal.h
@@ -165,6 +165,10 @@ inline bool PyIntScalar_Check(PyObject* obj) {
   return PyLong_Check(obj) || PyArray_IsScalar(obj, Integer);
 }
 
+inline bool PyBoolScalar_Check(PyObject* obj) {
+  return PyBool_Check(obj) || PyArray_IsScalar(obj, Bool);
+}
+
 }  // namespace internal
 
 }  // namespace py
diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd
index 7668117..f6589f9 100644
--- a/python/pyarrow/includes/libarrow.pxd
+++ b/python/pyarrow/includes/libarrow.pxd
@@ -1067,6 +1067,12 @@ cdef extern from 'arrow/python/config.h' namespace 'arrow::py':
     void set_numpy_nan(object o)
 
 
+cdef extern from 'arrow/python/inference.h' namespace 'arrow::py':
+    c_bool IsPyBool(object o)
+    c_bool IsPyInt(object o)
+    c_bool IsPyFloat(object o)
+
+
 cdef extern from 'arrow/python/benchmark.h' namespace 'arrow::py::benchmark':
     void Benchmark_PandasObjectIsNull(object lst) except *
 
diff --git a/python/pyarrow/tests/test_types.py b/python/pyarrow/tests/test_types.py
index 4aea73f..bd73fe0 100644
--- a/python/pyarrow/tests/test_types.py
+++ b/python/pyarrow/tests/test_types.py
@@ -16,8 +16,9 @@
 # under the License.
 
 from collections import OrderedDict
-import pickle
 
+import numpy as np
+import pickle
 import pytest
 
 import pyarrow as pa
@@ -471,3 +472,24 @@ def test_empty_table():
     assert isinstance(table, pa.Table)
     assert table.num_rows == 0
     assert table.schema == schema
+
+
+def test_is_integer_value():
+    assert pa.types.is_integer_value(1)
+    assert pa.types.is_integer_value(np.int64(1))
+    assert not pa.types.is_integer_value('1')
+
+
+def test_is_float_value():
+    assert not pa.types.is_float_value(1)
+    assert pa.types.is_float_value(1.)
+    assert pa.types.is_float_value(np.float64(1))
+    assert not pa.types.is_float_value('1.0')
+
+
+def test_is_boolean_value():
+    assert not pa.types.is_boolean_value(1)
+    assert pa.types.is_boolean_value(True)
+    assert pa.types.is_boolean_value(False)
+    assert pa.types.is_boolean_value(np.bool_(True))
+    assert pa.types.is_boolean_value(np.bool_(False))
diff --git a/python/pyarrow/types.pxi b/python/pyarrow/types.pxi
index 98c0dda..3ba36c9 100644
--- a/python/pyarrow/types.pxi
+++ b/python/pyarrow/types.pxi
@@ -1474,3 +1474,15 @@ def from_numpy_dtype(object dtype):
         check_status(NumPyDtypeToArrow(dtype, &c_type))
 
     return pyarrow_wrap_data_type(c_type)
+
+
+def is_boolean_value(object obj):
+    return IsPyBool(obj)
+
+
+def is_integer_value(object obj):
+    return IsPyInt(obj)
+
+
+def is_float_value(object obj):
+    return IsPyFloat(obj)
diff --git a/python/pyarrow/types.py b/python/pyarrow/types.py
index 6ce4386..2bd7027 100644
--- a/python/pyarrow/types.py
+++ b/python/pyarrow/types.py
@@ -17,6 +17,10 @@
 
 # Tools for dealing with Arrow type metadata in Python
 
+from pyarrow.lib import (is_boolean_value,  # noqa
+                         is_integer_value,
+                         is_float_value)
+
 import pyarrow.lib as lib