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

Function dataframe_to_arrays

python/pyarrow/pandas_compat.py:603–703  ·  view source on GitHub ↗
(df, schema, preserve_index, nthreads=1, columns=None,
                        safe=True)

Source from the content-addressed store, hash-verified

601
602
603def dataframe_to_arrays(df, schema, preserve_index, nthreads=1, columns=None,
604 safe=True):
605 (all_names,
606 column_names,
607 column_field_names,
608 index_column_names,
609 index_descriptors,
610 index_columns,
611 columns_to_convert,
612 convert_fields) = _get_columns_to_convert(df, schema, preserve_index,
613 columns)
614
615 # NOTE(wesm): If nthreads=None, then we use a heuristic to decide whether
616 # using a thread pool is worth it. Currently the heuristic is whether the
617 # nrows > 100 * ncols and ncols > 1.
618 if nthreads is None:
619 nrows, ncols = len(df), len(df.columns)
620 if nrows > ncols * 100 and ncols > 1:
621 nthreads = pa.cpu_count()
622 else:
623 nthreads = 1
624 # if we don't have threading in libarrow, don't use threading here either
625 if not is_threading_enabled():
626 nthreads = 1
627
628 def convert_column(col, field):
629 if field is None:
630 field_nullable = True
631 type_ = None
632 else:
633 field_nullable = field.nullable
634 type_ = field.type
635
636 try:
637 result = pa.array(col, type=type_, from_pandas=True, safe=safe)
638 except (pa.ArrowInvalid,
639 pa.ArrowNotImplementedError,
640 pa.ArrowTypeError) as e:
641 e.args += (
642 f"Conversion failed for column {col.name} with type {col.dtype}",)
643 raise e
644 if not field_nullable and result.null_count > 0:
645 raise ValueError(f"Field {field} was non-nullable but pandas column "
646 f"had {result.null_count} null values")
647 return result
648
649 def _can_definitely_zero_copy(ser):
650 if isinstance(ser.dtype, np.dtype):
651 arr = ser.values
652 return (isinstance(arr, np.ndarray) and
653 arr.flags.contiguous and
654 issubclass(arr.dtype.type, np.integer))
655 return False
656
657 if nthreads == 1:
658 arrays = [convert_column(c, f)
659 for c, f in zip(columns_to_convert, convert_fields)]
660 else:

Callers

nothing calls this directly

Calls 9

_get_columns_to_convertFunction · 0.85
lenFunction · 0.85
convert_columnFunction · 0.85
construct_metadataFunction · 0.85
appendMethod · 0.45
resultMethod · 0.45
fieldMethod · 0.45
schemaMethod · 0.45

Tested by

no test coverage detected