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

Function protocol_df_chunk_to_pyarrow

python/pyarrow/interchange/from_dataframe.py:121–165  ·  view source on GitHub ↗

Convert interchange protocol chunk to ``pa.RecordBatch``. Parameters ---------- df : DataFrameObject Object supporting the interchange protocol, i.e. `__dataframe__` method. allow_copy : bool, default: True Whether to allow copying the memory to perform

(
    df: DataFrameObject,
    allow_copy: bool = True
)

Source from the content-addressed store, hash-verified

119
120
121def protocol_df_chunk_to_pyarrow(
122 df: DataFrameObject,
123 allow_copy: bool = True
124) -> pa.RecordBatch:
125 """
126 Convert interchange protocol chunk to ``pa.RecordBatch``.
127
128 Parameters
129 ----------
130 df : DataFrameObject
131 Object supporting the interchange protocol, i.e. `__dataframe__`
132 method.
133 allow_copy : bool, default: True
134 Whether to allow copying the memory to perform the conversion
135 (if false then zero-copy approach is requested).
136
137 Returns
138 -------
139 pa.RecordBatch
140 """
141 # We need a dict of columns here, with each column being a pa.Array
142 columns: dict[str, pa.Array] = {}
143 for name in df.column_names():
144 if not isinstance(name, str):
145 raise ValueError(f"Column {name} is not a string")
146 if name in columns:
147 raise ValueError(f"Column {name} is not unique")
148 col = df.get_column_by_name(name)
149 dtype = col.dtype[0]
150 if dtype in (
151 DtypeKind.INT,
152 DtypeKind.UINT,
153 DtypeKind.FLOAT,
154 DtypeKind.STRING,
155 DtypeKind.DATETIME,
156 ):
157 columns[name] = column_to_array(col, allow_copy)
158 elif dtype == DtypeKind.BOOL:
159 columns[name] = bool_column_to_array(col, allow_copy)
160 elif dtype == DtypeKind.CATEGORICAL:
161 columns[name] = categorical_column_to_dictionary(col, allow_copy)
162 else:
163 raise NotImplementedError(f"Data type {dtype} not handled yet")
164
165 return pa.RecordBatch.from_pydict(columns)
166
167
168def column_to_array(

Callers 1

_from_dataframeFunction · 0.85

Calls 5

column_to_arrayFunction · 0.85
bool_column_to_arrayFunction · 0.85
get_column_by_nameMethod · 0.80
column_namesMethod · 0.45

Tested by

no test coverage detected