AlenkaF commented on code in PR #45818: URL: https://github.com/apache/arrow/pull/45818#discussion_r2062918249
########## python/pyarrow/scalar.pxi: ########## @@ -1064,13 +1126,26 @@ cdef class MapScalar(ListScalar): def __getitem__(self, i): """ - Return the value at the given index. + Return the value at the given index or key. """ + arr = self.values if arr is None: - raise IndexError(i) - dct = arr[_normalize_index(i, len(arr))] - return (dct[self.type.key_field.name], dct[self.type.item_field.name]) + raise IndexError(i) if isinstance(i, int) else KeyError(i) + + key_field = self.type.key_field.name + item_field = self.type.item_field.name + + if isinstance(i, int): + if arr is None: + raise IndexError(i) + dct = arr[_normalize_index(i, len(arr))] + return (dct[key_field], dct[item_field]) + else: + for dct in arr: + if dct[key_field].as_py() == i: + return dct[item_field] + raise KeyError(i) Review Comment: This can also be removed, same as above. ########## python/pyarrow/scalar.pxi: ########## @@ -1064,13 +1126,26 @@ cdef class MapScalar(ListScalar): def __getitem__(self, i): """ - Return the value at the given index. + Return the value at the given index or key. """ + arr = self.values if arr is None: - raise IndexError(i) - dct = arr[_normalize_index(i, len(arr))] - return (dct[self.type.key_field.name], dct[self.type.item_field.name]) + raise IndexError(i) if isinstance(i, int) else KeyError(i) + + key_field = self.type.key_field.name + item_field = self.type.item_field.name + + if isinstance(i, int): + if arr is None: + raise IndexError(i) Review Comment: I think this is not needed here anymore as it is already checked on line 1070/1133. ########## python/pyarrow/scalar.pxi: ########## @@ -847,6 +882,33 @@ cdef class BinaryScalar(Scalar): buffer = self.as_buffer() return None if buffer is None else buffer.to_pybytes() + def __bytes__(self): + return (self.as_py()) + + def __getbuffer__(self, cp.Py_buffer* buffer, int flags): + cdef Buffer buf = self.as_buffer() + + if buf.buffer.get().is_mutable(): + buffer.readonly = 0 + else: + if flags & cp.PyBUF_WRITABLE: + raise BufferError("Writable buffer requested but Arrow " + "buffer was not mutable") + buffer.readonly = 1 + buffer.buf = <char *>buf.buffer.get().data() + buffer.len = buf.size + if buffer.buf == NULL: + assert buffer.len == 0 + buffer.buf = cp.PyBytes_AS_STRING(b"") Review Comment: Could we test this part also, maybe with something like `pa.scalar(None, type=ty)`? ########## docs/source/python/compute.rst: ########## @@ -63,6 +63,21 @@ Below are a few simple examples:: >>> pc.multiply(x, y) <pyarrow.DoubleScalar: 72.54> +If you are using a compute function which returns more than one value, results +will be returned as a StructScalar. You can extract the individual values by Review Comment: ```suggestion will be returned as a ``StructScalar``. You can extract the individual values by ``` ########## docs/source/python/compute.rst: ########## @@ -63,6 +63,21 @@ Below are a few simple examples:: >>> pc.multiply(x, y) <pyarrow.DoubleScalar: 72.54> +If you are using a compute function which returns more than one value, results Review Comment: ❤️ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org