MCPcopy Create free account
hub / github.com/apache/arrow / ConvertMap

Function ConvertMap

python/pyarrow/src/arrow/python/arrow_to_pandas.cc:977–1082  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

975}
976
977Status ConvertMap(PandasOptions options, const ChunkedArray& data,
978 PyObject** out_values) {
979 // Get columns of underlying key/item arrays
980 std::vector<std::shared_ptr<Array>> key_arrays;
981 std::vector<std::shared_ptr<Array>> item_arrays;
982 for (int c = 0; c < data.num_chunks(); ++c) {
983 const auto& map_arr = checked_cast<const MapArray&>(*data.chunk(c));
984 key_arrays.emplace_back(map_arr.keys());
985 item_arrays.emplace_back(map_arr.items());
986 }
987
988 const auto& map_type = checked_cast<const MapType&>(*data.type());
989 auto key_type = map_type.key_type();
990 auto item_type = map_type.item_type();
991
992 // ARROW-6899: Convert dictionary-encoded children to dense instead of
993 // failing below. A more efficient conversion than this could be done later
994 if (key_type->id() == Type::DICTIONARY) {
995 auto dense_type = checked_cast<const DictionaryType&>(*key_type).value_type();
996 RETURN_NOT_OK(DecodeDictionaries(options.pool, dense_type, &key_arrays));
997 key_type = dense_type;
998 }
999 if (item_type->id() == Type::DICTIONARY) {
1000 auto dense_type = checked_cast<const DictionaryType&>(*item_type).value_type();
1001 RETURN_NOT_OK(DecodeDictionaries(options.pool, dense_type, &item_arrays));
1002 item_type = dense_type;
1003 }
1004
1005 // See notes in MakeInnerOptions.
1006 options = MakeInnerOptions(std::move(options));
1007 // Don't blindly convert because timestamps in lists are handled differently.
1008 options.timestamp_as_object = true;
1009
1010 auto flat_keys = std::make_shared<ChunkedArray>(key_arrays, key_type);
1011 auto flat_items = std::make_shared<ChunkedArray>(item_arrays, item_type);
1012 OwnedRefNoGIL owned_numpy_keys;
1013 RETURN_NOT_OK(
1014 ConvertChunkedArrayToPandas(options, flat_keys, nullptr, owned_numpy_keys.ref()));
1015 OwnedRefNoGIL owned_numpy_items;
1016 RETURN_NOT_OK(
1017 ConvertChunkedArrayToPandas(options, flat_items, nullptr, owned_numpy_items.ref()));
1018 PyArrayObject* py_keys = reinterpret_cast<PyArrayObject*>(owned_numpy_keys.obj());
1019 PyArrayObject* py_items = reinterpret_cast<PyArrayObject*>(owned_numpy_items.obj());
1020
1021 if (options.maps_as_pydicts == MapConversionType::DEFAULT) {
1022 // The default behavior to express an Arrow MAP as a list of [(key, value), ...] pairs
1023 OwnedRef list_item;
1024 return ConvertMapHelper(
1025 [&list_item](int64_t num_pairs) {
1026 list_item.reset(PyList_New(num_pairs));
1027 return CheckPyError();
1028 },
1029 [&list_item](int64_t idx, OwnedRef& key_value, OwnedRef& item_value) {
1030 PyList_SET_ITEM(list_item.obj(), idx,
1031 PyTuple_Pack(2, key_value.obj(), item_value.obj()));
1032 return CheckPyError();
1033 },
1034 [&list_item] { return list_item.detach(); }, data, py_keys, py_items, item_arrays,

Callers 1

VisitMethod · 0.85

Calls 15

DecodeDictionariesFunction · 0.85
MakeInnerOptionsFunction · 0.85
ConvertMapHelperFunction · 0.85
CheckPyErrorFunction · 0.85
UnknownErrorFunction · 0.85
to_stringFunction · 0.85
CheckForDuplicateKeysFunction · 0.85
emplace_backMethod · 0.80
OKFunction · 0.50
num_chunksMethod · 0.45

Tested by

no test coverage detected