From c9d0f908d1895d0abb5b89589abf277d62cdd4fb Mon Sep 17 00:00:00 2001 From: aandres Date: Sun, 23 Jul 2023 14:24:51 +0100 Subject: [PATCH 1/2] Map scalar as py with custom field name --- cpp/submodules/parquet-testing | 2 +- python/pyarrow/scalar.pxi | 4 ++-- python/pyarrow/tests/test_scalars.py | 23 +++++++++++++++++++++++ testing | 2 +- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cpp/submodules/parquet-testing b/cpp/submodules/parquet-testing index b2e7cc7551591..d79a0101d90df 160000 --- a/cpp/submodules/parquet-testing +++ b/cpp/submodules/parquet-testing @@ -1 +1 @@ -Subproject commit b2e7cc755159196e3a068c8594f7acbaecfdaaac +Subproject commit d79a0101d90dfa3bbb10337626f57a3e8c4b5363 diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi index f438c8847bb02..74f5aa4213ca0 100644 --- a/python/pyarrow/scalar.pxi +++ b/python/pyarrow/scalar.pxi @@ -785,7 +785,7 @@ cdef class MapScalar(ListScalar): if arr is None: raise IndexError(i) dct = arr[_normalize_index(i, len(arr))] - return (dct['key'], dct['value']) + return (dct[self.type.key_field.name], dct[self.type.item_field.name]) def __iter__(self): """ @@ -794,7 +794,7 @@ cdef class MapScalar(ListScalar): arr = self.values if array is None: raise StopIteration - for k, v in zip(arr.field('key'), arr.field('value')): + for k, v in zip(arr.field(self.type.key_field.name), arr.field(self.type.item_field.name)): yield (k.as_py(), v.as_py()) def as_py(self): diff --git a/python/pyarrow/tests/test_scalars.py b/python/pyarrow/tests/test_scalars.py index b7180e5250fdf..2aaefe16ae469 100644 --- a/python/pyarrow/tests/test_scalars.py +++ b/python/pyarrow/tests/test_scalars.py @@ -791,3 +791,26 @@ def test_union(): assert arr[0].as_py() == b'a' assert arr[5].type_code == 1 assert arr[5].as_py() == 3 + + +def test_map_scalar_as_py_with_custom_field_name(): + """ + Check we can call `MapScalar.as_py` with custom field names + + See https://github.com/apache/arrow/issues/36809 + """ + assert pa.scalar( + [("foo", "bar")], + pa.map_( + pa.string(), + pa.string() + ), + ).as_py() == [("foo", "bar")] + + assert pa.scalar( + [("foo", "bar")], + pa.map_( + pa.field("custom_key", pa.string(), nullable=False), + pa.field("custom_value", pa.string()), + ), + ).as_py() == [("foo", "bar")] diff --git a/testing b/testing index 47f7b56b25683..e81d0c6de3594 160000 --- a/testing +++ b/testing @@ -1 +1 @@ -Subproject commit 47f7b56b25683202c1fd957668e13f2abafc0f12 +Subproject commit e81d0c6de35948b3be7984af8e00413b314cde6e From 5bfe9a5c356112b0a8c513a5ab4ee6b0a2328405 Mon Sep 17 00:00:00 2001 From: aandres Date: Mon, 24 Jul 2023 09:05:24 +0100 Subject: [PATCH 2/2] Rollback submodule update --- cpp/submodules/parquet-testing | 2 +- testing | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/submodules/parquet-testing b/cpp/submodules/parquet-testing index d79a0101d90df..b2e7cc7551591 160000 --- a/cpp/submodules/parquet-testing +++ b/cpp/submodules/parquet-testing @@ -1 +1 @@ -Subproject commit d79a0101d90dfa3bbb10337626f57a3e8c4b5363 +Subproject commit b2e7cc755159196e3a068c8594f7acbaecfdaaac diff --git a/testing b/testing index e81d0c6de3594..47f7b56b25683 160000 --- a/testing +++ b/testing @@ -1 +1 @@ -Subproject commit e81d0c6de35948b3be7984af8e00413b314cde6e +Subproject commit 47f7b56b25683202c1fd957668e13f2abafc0f12