This is an automated email from the ASF dual-hosted git repository.
jorisvandenbossche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 734059e29b ARROW-17832: [Python] Construct MapArray from sequence of
dicts (instead of list of tuples) (#14547)
734059e29b is described below
commit 734059e29be6f731f93162d048e2a0df59c73896
Author: Kshiteej K <[email protected]>
AuthorDate: Wed Nov 9 15:36:57 2022 +0530
ARROW-17832: [Python] Construct MapArray from sequence of dicts (instead of
list of tuples) (#14547)
Following snippet now works.
```python
dict_arr = pa.array([{'a': 1, 'b': 2}, {'c': 3}],
pa.map_(pa.string(), pa.int64()))
```
Lead-authored-by: Kshiteej K <[email protected]>
Co-authored-by: kshitij12345 <[email protected]>
Co-authored-by: Joris Van den Bossche <[email protected]>
Signed-off-by: Joris Van den Bossche <[email protected]>
---
python/pyarrow/src/arrow/python/python_to_arrow.cc | 5 +++++
python/pyarrow/tests/test_array.py | 10 ++++++++++
2 files changed, 15 insertions(+)
diff --git a/python/pyarrow/src/arrow/python/python_to_arrow.cc
b/python/pyarrow/src/arrow/python/python_to_arrow.cc
index 3a43caed11..ffba72c20f 100644
--- a/python/pyarrow/src/arrow/python/python_to_arrow.cc
+++ b/python/pyarrow/src/arrow/python/python_to_arrow.cc
@@ -760,6 +760,11 @@ class PyListConverter : public ListConverter<T,
PyConverter, PyConverterTrait> {
RETURN_NOT_OK(AppendSequence(value));
} else if (PySet_Check(value) || (Py_TYPE(value) == &PyDictValues_Type)) {
RETURN_NOT_OK(AppendIterable(value));
+ } else if (PyDict_Check(value) && this->options_.type->id() == Type::MAP) {
+ // Branch to support Python Dict with `map` DataType.
+ auto items = PyDict_Items(value);
+ OwnedRef item_ref(items);
+ RETURN_NOT_OK(AppendSequence(items));
} else {
return internal::InvalidType(
value, "was not a sequence or recognized null for conversion to list
type");
diff --git a/python/pyarrow/tests/test_array.py
b/python/pyarrow/tests/test_array.py
index 154ca348d2..6560466891 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -991,6 +991,16 @@ def test_map_labelled():
assert len(arr) == 2
+def test_map_from_dict():
+ # ARROW-17832
+ tup_arr = pa.array([[('a', 1), ('b', 2)], [('c', 3)]],
+ pa.map_(pa.string(), pa.int64()))
+ dict_arr = pa.array([{'a': 1, 'b': 2}, {'c': 3}],
+ pa.map_(pa.string(), pa.int64()))
+
+ assert tup_arr.equals(dict_arr)
+
+
def test_map_from_arrays():
offsets_arr = np.array([0, 2, 5, 8], dtype='i4')
offsets = pa.array(offsets_arr, type='int32')