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

Function ConvertStruct

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

Source from the content-addressed store, hash-verified

673}
674
675Status ConvertStruct(PandasOptions options, const ChunkedArray& data,
676 PyObject** out_values) {
677 if (data.num_chunks() == 0) {
678 return Status::OK();
679 }
680 // ChunkedArray has at least one chunk
681 auto arr = checked_cast<const StructArray*>(data.chunk(0).get());
682 // Use it to cache the struct type and number of fields for all chunks
683 int32_t num_fields = arr->num_fields();
684 auto array_type = arr->type();
685 std::vector<OwnedRef> fields_data(num_fields * data.num_chunks());
686 OwnedRef dict_item;
687
688 // See notes in MakeInnerOptions.
689 options = MakeInnerOptions(std::move(options));
690 // Don't blindly convert because timestamps in lists are handled differently.
691 options.timestamp_as_object = true;
692
693 for (int c = 0; c < data.num_chunks(); c++) {
694 auto fields_data_offset = c * num_fields;
695 auto arr = checked_cast<const StructArray*>(data.chunk(c).get());
696 // Convert the struct arrays first
697 for (int32_t i = 0; i < num_fields; i++) {
698 auto field = arr->field(static_cast<int>(i));
699 // In case the field is an extension array, use .storage() to convert to Pandas
700 if (field->type()->id() == Type::EXTENSION) {
701 const ExtensionArray& arr_ext = checked_cast<const ExtensionArray&>(*field);
702 field = arr_ext.storage();
703 }
704 RETURN_NOT_OK(ConvertArrayToPandas(options, field, nullptr,
705 fields_data[i + fields_data_offset].ref()));
706 ARROW_DCHECK(PyArray_Check(fields_data[i + fields_data_offset].obj()));
707 }
708
709 // Construct a dictionary for each row
710 const bool has_nulls = data.null_count() > 0;
711 for (int64_t i = 0; i < arr->length(); ++i) {
712 if (has_nulls && arr->IsNull(i)) {
713 Py_INCREF(Py_None);
714 *out_values = Py_None;
715 } else {
716 // Build the new dict object for the row
717 dict_item.reset(PyDict_New());
718 RETURN_IF_PYERROR();
719 for (int32_t field_idx = 0; field_idx < num_fields; ++field_idx) {
720 OwnedRef field_value;
721 auto name = array_type->field(static_cast<int>(field_idx))->name();
722 if (!arr->field(static_cast<int>(field_idx))->IsNull(i)) {
723 // Value exists in child array, obtain it
724 auto array = reinterpret_cast<PyArrayObject*>(
725 fields_data[field_idx + fields_data_offset].obj());
726 auto ptr = reinterpret_cast<const char*>(PyArray_GETPTR1(array, i));
727 field_value.reset(PyArray_GETITEM(array, ptr));
728 RETURN_IF_PYERROR();
729 } else {
730 // Translate the Null to a None
731 Py_INCREF(Py_None);
732 field_value.reset(Py_None);

Callers 1

VisitMethod · 0.85

Calls 14

MakeInnerOptionsFunction · 0.85
ConvertArrayToPandasFunction · 0.85
OKFunction · 0.50
num_chunksMethod · 0.45
getMethod · 0.45
num_fieldsMethod · 0.45
typeMethod · 0.45
fieldMethod · 0.45
idMethod · 0.45
null_countMethod · 0.45
lengthMethod · 0.45
IsNullMethod · 0.45

Tested by

no test coverage detected