AlenkaF commented on code in PR #45818:
URL: https://github.com/apache/arrow/pull/45818#discussion_r2063573529


##########
python/pyarrow/scalar.pxi:
##########
@@ -1064,13 +1129,24 @@ 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):
+            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 is what I would suggest:
   
   ```suggestion
           if isinstance(i, (str, bytes)):
               i = self.keys().index(i)
   
           dct = arr[_normalize_index(i, len(arr))]
           return (dct[key_field], dct[item_field])
   ```
   
   There will be two changes in behaviour though. The key lookup will return a 
key and value tuple and not only a value which is inline with the index lookup 
and I think it is valid. The error in case the key is not present (like 
"fake_key") will be raised by `index(i)` but with `ValueError`. That can be 
changed though.



-- 
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

Reply via email to