Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-36809: [Python] MapScalar.as_py with custom field name #36830

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/pyarrow/scalar.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
Expand Down
23 changes: 23 additions & 0 deletions python/pyarrow/tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]