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


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

Review Comment:
   Given that looping over all keys is not efficient, I wonder if it's really a 
good idea to provide this after all.
   @jorisvandenbossche @kszucs  Thoughts?
   



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