[ 
https://issues.apache.org/jira/browse/ARROW-640?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16414620#comment-16414620
 ] 

ASF GitHub Bot commented on ARROW-640:
--------------------------------------

wesm closed pull request #1765: ARROW-640: [Python] Implement __hash__ and 
equality for Array scalar values Arrow scalar values
URL: https://github.com/apache/arrow/pull/1765
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi
index a801acd69..bbbefd834 100644
--- a/python/pyarrow/scalar.pxi
+++ b/python/pyarrow/scalar.pxi
@@ -73,6 +73,8 @@ cdef class ArrayValue(Scalar):
             raise NotImplementedError(
                 "Cannot compare Arrow values that don't support as_py()")
 
+    def __hash__(self):
+            return hash(self.as_py())
 
 cdef class BooleanValue(ArrayValue):
 
diff --git a/python/pyarrow/tests/test_scalars.py 
b/python/pyarrow/tests/test_scalars.py
index 7061a0d3a..92db9b1e0 100644
--- a/python/pyarrow/tests/test_scalars.py
+++ b/python/pyarrow/tests/test_scalars.py
@@ -171,3 +171,30 @@ def test_dictionary(self):
                                            categorical.categories)
         for i, c in enumerate(values):
             assert v[i].as_py() == c
+
+    def test_int_hash(self):
+        # ARROW-640
+        int_arr = pa.array([1, 1, 2, 1])
+        assert hash(int_arr[0]) == hash(1)
+
+    def test_float_hash(self):
+        # ARROW-640
+        float_arr = pa.array([1.4, 1.2, 2.5, 1.8])
+        assert hash(float_arr[0]) == hash(1.4)
+
+    def test_string_hash(self):
+        # ARROW-640
+        str_arr = pa.array(["foo", "bar"])
+        assert hash(str_arr[1]) == hash("bar")
+
+    def test_bytes_hash(self):
+        # ARROW-640
+        byte_arr = pa.array([b'foo', None, b'bar'])
+        assert hash(byte_arr[2]) == hash(b"bar")
+
+    def test_array_to_set(self):
+        # ARROW-640
+        arr = pa.array([1, 1, 2, 1])
+        set_from_array = set(arr)
+        assert isinstance(set_from_array, set)
+        assert set_from_array == {1, 2}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Python] Arrow scalar values should have a sensible __hash__ and comparison
> ---------------------------------------------------------------------------
>
>                 Key: ARROW-640
>                 URL: https://issues.apache.org/jira/browse/ARROW-640
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Python
>            Reporter: Miki Tebeka
>            Assignee: Alex Hagerman
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.10.0
>
>
> {noformat}
> In [86]: arr = pa.from_pylist([1, 1, 1, 2])
> In [87]: set(arr)
> Out[87]: {1, 2, 1, 1}
> In [88]: arr[0] == arr[1]
> Out[88]: False
> In [89]: arr
> Out[89]: 
> <pyarrow.array.Int64Array object at 0x7f8c8c739e08>
> [
>   1,
>   1,
>   1,
>   2
> ]
> {noformat}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to