You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/09/14 19:54:16 UTC
[GitHub] [arrow] drin commented on issue #14116: Python dict to map
drin commented on issue #14116:
URL: https://github.com/apache/arrow/issues/14116#issuecomment-1247235473
I believe the problem is that you are incorrectly providing data to `from_pylist` and you are incorrectly structuring the data for `tags`.
Here is a code snippet that I think does what you're looking for:
```python
from pyarrow import Table
from pyarrow import int64, map_, schema, string, field
def test_map_type():
# 2 column schema; a map array is a list of tuples
pyarrow_schema = schema([
field('id' , int64() , False)
,field('tags', map_(string(), string()), False)
])
# each row should have: <key count> <= <column count>
first_row ={
'id' : 1
,'tags': [
('tag1', 'value1')
,('tag2', 'value2' )
]
}
second_row = {
'id' : 2
,'tags': [
('tag1', 'value1')
,('tag2', 'value2' )
]
}
tags_updated = [first_row, second_row]
table = Table.from_pylist(mapping=tags_updated, schema=pyarrow_schema)
print(table.to_pydict())
test_map_type()
```
output:
```bash
>> python test.py
{'id': [1, 2], 'tags': [[('tag1', 'value1'), ('tag2', 'value2')], [('tag1', 'value1'), ('tag2', 'value2')]]}
```
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org