Skip to content

[Python] Scalar constructors should be callable #31648

@asfimport

Description

@asfimport

Currently, to create a Scalar instance you must call the pa.scalar factory function. This works fine for the simplest types, but not for more elaborate ones.

For example, in C++ it is trivial to create a ListScalar from the embedded std::shared_ptr<Array>. But not in Python:

>>> arr = pa.array(["abc", "def", "ghi"])
>>> pa.ListScalar(arr)
Traceback (most recent call last):
  Input In [7] in <cell line: 1>
    pa.ListScalar(arr)
TypeError: __init__() takes exactly 0 positional arguments (1 given)

There is a clumsy workaround that creates an intermediate ListArray with trivial offsets:

>>> list_arr = pa.ListArray.from_arrays([0, len(arr)], arr)
>>> list_arr[0]
<pyarrow.ListScalar: ['abc', 'def', 'ghi']>

Reporter: Antoine Pitrou / @pitrou

Related issues:

Note: This issue was originally created as ARROW-16254. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions