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

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

xhochy closed pull request #1716: ARROW-2154: [Python] Implement equality on 
buffers
URL: https://github.com/apache/arrow/pull/1716
 
 
   

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/array.pxi b/python/pyarrow/array.pxi
index d4e53ecd5..7899d9dbb 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -541,6 +541,12 @@ strides: {0.strides}""".format(self)
         self._validate()
         return self.tp.Equals(deref(other.tp))
 
+    def __eq__(self, other):
+        if isinstance(other, Tensor):
+            return self.equals(other)
+        else:
+            return NotImplemented
+
     @property
     def is_mutable(self):
         self._validate()
@@ -565,12 +571,12 @@ strides: {0.strides}""".format(self)
     def shape(self):
         # Cython knows how to convert a vector[T] to a Python list
         self._validate()
-        return self.tp.shape()
+        return tuple(self.tp.shape())
 
     @property
     def strides(self):
         self._validate()
-        return self.tp.strides()
+        return tuple(self.tp.strides())
 
 
 cdef wrap_array_output(PyObject* output):
diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi
index 5c8411be4..611c8a86d 100644
--- a/python/pyarrow/io.pxi
+++ b/python/pyarrow/io.pxi
@@ -671,6 +671,12 @@ cdef class Buffer:
             result = self.buffer.get().Equals(deref(other.buffer.get()))
         return result
 
+    def __eq__(self, other):
+        if isinstance(other, Buffer):
+            return self.equals(other)
+        else:
+            return NotImplemented
+
     def to_pybytes(self):
         self._check_nullptr()
         return cp.PyBytes_FromStringAndSize(
diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py
index 17aca4333..e42914ffc 100644
--- a/python/pyarrow/tests/test_io.py
+++ b/python/pyarrow/tests/test_io.py
@@ -237,6 +237,16 @@ def test_buffer_from_numpy():
 
 def test_buffer_equals():
     # Buffer.equals() returns true iff the buffers have the same contents
+    def eq(a, b):
+        assert a.equals(b)
+        assert a == b
+        assert not (a != b)
+
+    def ne(a, b):
+        assert not a.equals(b)
+        assert not (a == b)
+        assert a != b
+
     b1 = b'some data!'
     b2 = bytearray(b1)
     b3 = bytearray(b1)
@@ -246,12 +256,18 @@ def test_buffer_equals():
     buf3 = pa.frombuffer(b2)
     buf4 = pa.frombuffer(b3)
     buf5 = pa.frombuffer(np.frombuffer(b2, dtype=np.int16))
-    assert buf1.equals(buf1)
-    assert buf1.equals(buf2)
-    assert buf2.equals(buf3)
-    assert not buf2.equals(buf4)
+    eq(buf1, buf1)
+    eq(buf1, buf2)
+    eq(buf2, buf3)
+    ne(buf2, buf4)
     # Data type is indifferent
-    assert buf2.equals(buf5)
+    eq(buf2, buf5)
+
+
+def test_buffer_hashing():
+    # Buffers are unhashable
+    with pytest.raises(TypeError, match="unhashable"):
+        hash(pa.frombuffer(b'123'))
 
 
 def test_foreign_buffer():
diff --git a/python/pyarrow/tests/test_tensor.py 
b/python/pyarrow/tests/test_tensor.py
index 1d45dc743..093bc86c3 100644
--- a/python/pyarrow/tests/test_tensor.py
+++ b/python/pyarrow/tests/test_tensor.py
@@ -30,8 +30,8 @@ def test_tensor_attrs():
 
     assert tensor.ndim == 2
     assert tensor.size == 40
-    assert tensor.shape == list(data.shape)
-    assert tensor.strides == list(data.strides)
+    assert tensor.shape == data.shape
+    assert tensor.strides == data.strides
 
     assert tensor.is_contiguous
     assert tensor.is_mutable
@@ -121,14 +121,30 @@ def test_tensor_ipc_strided(tmpdir):
 
 
 def test_tensor_equals():
+    def eq(a, b):
+        assert a.equals(b)
+        assert a == b
+        assert not (a != b)
+
+    def ne(a, b):
+        assert not a.equals(b)
+        assert not (a == b)
+        assert a != b
+
     data = np.random.randn(10, 6, 4)[::, ::2, ::]
     tensor1 = pa.Tensor.from_numpy(data)
     tensor2 = pa.Tensor.from_numpy(np.ascontiguousarray(data))
-    assert tensor1.equals(tensor2)
+    eq(tensor1, tensor2)
     data = data.copy()
     data[9, 0, 0] = 1.0
     tensor2 = pa.Tensor.from_numpy(np.ascontiguousarray(data))
-    assert not tensor1.equals(tensor2)
+    ne(tensor1, tensor2)
+
+
+def test_tensor_hashing():
+    # Tensors are unhashable
+    with pytest.raises(TypeError, match="unhashable"):
+        hash(pa.Tensor.from_numpy(np.arange(10)))
 
 
 def test_tensor_size():


 

----------------------------------------------------------------
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] __eq__ unimplemented on Buffer
> ---------------------------------------
>
>                 Key: ARROW-2154
>                 URL: https://issues.apache.org/jira/browse/ARROW-2154
>             Project: Apache Arrow
>          Issue Type: Improvement
>          Components: Python
>    Affects Versions: 0.8.0
>            Reporter: Antoine Pitrou
>            Priority: Minor
>              Labels: pull-request-available
>
> Having to call {{equals()}} is un-Pythonic:
> {code:python}
> >>> pa.frombuffer(b'foo') == pa.frombuffer(b'foo')
> False
> >>> pa.frombuffer(b'foo').equals(pa.frombuffer(b'foo'))
> True
> {code}
> Same for many other pyarrow types, incidently.



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

Reply via email to