Skip to content

Commit

Permalink
pa.scalar working for extension types
Browse files Browse the repository at this point in the history
  • Loading branch information
joellubi committed Aug 12, 2024
1 parent 6842980 commit 2e2efcf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/pyarrow/scalar.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,11 @@ def scalar(value, type=None, *, from_pandas=None, MemoryPool memory_pool=None):
type = ensure_type(type, allow_none=True)
pool = maybe_unbox_memory_pool(memory_pool)

extension_type = None
if type is not None and type.id == _Type_EXTENSION:
extension_type = type
type = type.storage_type

if _is_array_like(value):
value = get_values(value, &is_pandas_object)

Expand All @@ -1228,4 +1233,8 @@ def scalar(value, type=None, *, from_pandas=None, MemoryPool memory_pool=None):

# retrieve the scalar from the first position
scalar = GetResultValue(array.get().GetScalar(0))
return Scalar.wrap(scalar)
result = Scalar.wrap(scalar)

if extension_type is not None:
result = ExtensionScalar.from_storage(extension_type, result)
return result
6 changes: 6 additions & 0 deletions python/pyarrow/tests/test_extension_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -1801,3 +1801,9 @@ def test_bool8_scalar():
assert arr[2].as_py()
assert arr[3].as_py()
assert arr[4].as_py() is None

assert pa.scalar(-1, type=pa.bool8()).as_py()
assert not pa.scalar(0, type=pa.bool8()).as_py()
assert pa.scalar(1, type=pa.bool8()).as_py()
assert pa.scalar(2, type=pa.bool8()).as_py()
assert pa.scalar(None, type=pa.bool8()).as_py() is None

0 comments on commit 2e2efcf

Please sign in to comment.