Skip to content

Commit 161510e

Browse files
authored
GH-37145: [Python] support boolean columns with bitsize 1 in from_dataframe (#37975)
### Rationale for this change Bit-packed booleans are currently not supported in the `from_dataframe` of the Dataframe Interchange Protocol. Note: We currently represent booleans in the pyarrow implementation as `uint8` which will also need to be changed in a follow-up PR (see data-apis/dataframe-api#227). ### What changes are included in this PR? This PR adds the support for bit-packed booleans when consuming a dataframe interchange object. ### Are these changes tested? Only locally, currently! * Closes: #37145 Lead-authored-by: AlenkaF <[email protected]> Co-authored-by: Alenka Frim <[email protected]> Signed-off-by: AlenkaF <[email protected]>
1 parent f7530de commit 161510e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

python/pyarrow/interchange/from_dataframe.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
DtypeKind.FLOAT: {16: pa.float16(),
5555
32: pa.float32(),
5656
64: pa.float64()},
57-
DtypeKind.BOOL: {8: pa.uint8()},
57+
DtypeKind.BOOL: {1: pa.bool_(),
58+
8: pa.uint8()},
5859
DtypeKind.STRING: {8: pa.string()},
5960
}
6061

@@ -232,19 +233,23 @@ def bool_column_to_array(
232233
-------
233234
pa.Array
234235
"""
235-
if not allow_copy:
236+
buffers = col.get_buffers()
237+
size = buffers["data"][1][1]
238+
239+
# If booleans are byte-packed a copy to bit-packed will be made
240+
if size == 8 and not allow_copy:
236241
raise RuntimeError(
237242
"Boolean column will be casted from uint8 and a copy "
238243
"is required which is forbidden by allow_copy=False"
239244
)
240245

241-
buffers = col.get_buffers()
242246
data_type = col.dtype
243247
data = buffers_to_array(buffers, data_type,
244248
col.size(),
245249
col.describe_null,
246250
col.offset)
247-
data = pc.cast(data, pa.bool_())
251+
if size == 8:
252+
data = pc.cast(data, pa.bool_())
248253

249254
return data
250255

0 commit comments

Comments
 (0)